0

I have a class with various members that I only assign at the constructor phase:

class Pet {
    const int id;
    std::string name;

    Pet(int iid, std::string nname) {
        id = iid;
        if (nname != "") { name = nname; }
        else { name = "Johnny Doey"; }
    }

    Pet() : Pet{ -1, "" } {}
}

I'm fairly new at C++, but wouldn't it make sense to make these members const? However, simply adding the const keyword doesn't work ("expression must be a modifiable lvalue"), so how would I do it?

Enlico
  • 23,259
  • 6
  • 48
  • 102
Markstar
  • 639
  • 9
  • 24
  • 1
    Yes, you should make those members `const` if they are not meant to be modified after the object is constructed. You can do that initialization in a member-initializer-list. – cigien Dec 17 '20 at 17:49
  • Note that some care must be taken with assigning to `name`. You can use the [ternary operator](https://en.cppreference.com/w/cpp/language/operator_other#Conditional_operator) in the member-initializer list to make sure you perform the correct assignment at initialization. – Nathan Pierson Dec 17 '20 at 17:50
  • const members are problematic because they prevent copy assignment and construction. consider making them private and adding accessors. – Thomas Dec 17 '20 at 19:29
  • It's too bad this is closed. I actually did read the link that is given as duplicate, but I don't know how exactly I would use this in this example (e.g. with two or more member variables). :( @Thomas: Hmm, in another thread I saw that setters and getters are bad and a sign of bad programming. :/ – Markstar Dec 17 '20 at 20:36
  • 1
    @Markstar imho using them to make an object immutable is a perfectly good use case. [Idiomatic Way to declare C++ Immutable Classes](https://stackoverflow.com/questions/26858518/idiomatic-way-to-declare-c-immutable-classes) – Thomas Dec 17 '20 at 22:33
  • 1
    see [member initializer lists](https://en.cppreference.com/w/cpp/language/constructor) for initializing multiple members. Setting them inside the constructor is too late, the objects are already const. – Thomas Dec 17 '20 at 22:40
  • 1
    [maybe this helps](http://coliru.stacked-crooked.com/a/b49299c2171a2e1f) – Thomas Dec 17 '20 at 22:54
  • @Thomas: Thank you VERY MUCH!!! for your replies! Regarding the initializer list: Thank you, I will see how far I can stretch this concept. I did plan on doing some calculations before assigning some values (e.g. integrating the id and some other info into the name if one isn't given). An alternative may be a factory method of some sort. – Markstar Dec 18 '20 at 20:26
  • Regarding the code, could you please tell me why in line 12 there is a `const` at the end of the line? Thanks again! (also, I notice, that the variables themselves are not `const`) – Markstar Dec 18 '20 at 20:27
  • it is considered good practice to mark members that don't modify an object const. that is also a prerequisite to calling the method on a const object. see [Const Correctness](https://isocpp.org/wiki/faq/const-correctness) – Thomas Dec 18 '20 at 22:25

0 Answers0