missingfaktor
missingfaktor

Reputation: 92076

Are the following two statements semantically same?

Are the following two statements semantically same?

#1 person p("Rahul", 20);

#2 person const &p = person("Rahul", 20);

EDIT:

Sorry, I meant to ask whether the following two are semantically same:

#1 person const p("Rahul", 20);

#2 person const &p = person("Rahul", 20);

Upvotes: 1

Views: 191

Answers (2)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507225

They are not. However, the difference are affected only by the fact that the second case needs a copy constructor to be accessible in C++03 (even if the copy constructor call is not actually done)

// works with #1 fails with #2
struct f1 { f1(string, int); private: f1(f1 const&); };

Upvotes: 2

anon
anon

Reputation:

No they are not. The way that p behaves in each case is different. For example, in the latter case, you could not say:

p.rename( "fred" );

assuming person had a rename() method.

Of course, if your first instance had been:

const person p("Rahul", 20);

the two would have been much more similar. I hope you are not intending using references for all your "variables" :-)

Upvotes: 5

Related Questions