Hi guys,

I understand the difference is that the params of ctor 2 are declared const with n being passed by reference.

However, I don't seem to understand what difference having these params (n and a) declared constant with n being passed by reference makes?

I mean, I'd understand the effect if the private members of 'name' and 'age' were declared constant, just not why n/a are?

Person(string n, int a) : name(n), age(a) {} // ctor 1

Person(const string &n, const int a) : name(n), age(a) {} // ctor 2

Recommended Answers

All 5 Replies

Hi guys,

I understand the difference is that the params of ctor 2 are declared const with n being passed by reference.

However, I don't seem to understand what difference having these params (n and a) declared constant with n being passed by reference makes?

I mean, I'd understand the effect if the private members of 'name' and 'age' were declared constant, just not why n/a are?

Person(string n, int a) : name(n), age(a) {} // ctor 1

Person(const string &n, const int a) : name(n), age(a) {} // ctor 2

By passing a const reference, you make a read-only reference. By making a reference you don't copy the data in the variable, so you save memory, and you don't risk to change the content of the original variable, since you make it read only.

Correct me if I'm wrong...

So since the params in the constructor are only used to initialise the name and age variables, declaring them as const/reference is more a safety/performance advantage as opposed to any difference in functionality?

("difference in functionality" - for example, if name and age were declared const, they would not be able to be mutated later on in the program)

Correct me if I'm wrong...

So since the params in the constructor are only used to initialise the name and age variables, declaring them as const/reference is more a safety/performance advantage as opposed to any difference in functionality?

("difference in functionality" - for example, if name and age were declared const, they would not be able to be mutated later on in the program)

Indeed that is correct.

When you then copy the data from the const reference parameters, it will still be the same data, and the type of the new variable you create is not affected.

To summarize, not using reference would mean something like this:

std::string name = "something"; // < A string is created and is now taking up memory
classe(name); // < classe(string inputName); is making a new string with the same content as name.
classe(string inputName) : classname(inputName){} // < finally, the copied string is now copied once again

This is odd, to make 3 strings, for the same data, when you only need two, maybe even just one.
So using references is the way to do it to win performance.

Maybe you later add functionality to the ctor, and you might risk changing inputName, which is a reference to the original string. So you change the original string, which may be used by several other functions too *gasp*. So by making it const, you make sure you are unable to do that.

Thanks for the detailed response. :-)

Glad to help :)
Happy new year :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.