Questions of reference

S

Stub

Question 1:

class Count
{
public:
Count() : id(n++) { cout << id << " default ctor" << endl; }
Count(Count const&) : id(n++) { cout << id << " copy ctor" << endl;
Count (Count const& src) : v(src.v) { cout << "Count(Count const&) " << v
<< "\n"; }
};


void func(int& x) {//non-const ref
cout << x << endl;
}

What're the implication of "Count(Count const&)" above? Is it the same as
"Count(const &Count)"? Does it have anything to do with "Count(const Count
&c)"?


Question 2:

int main (){
const int My_const = 68;

//This works
func(const_cast<int&>(My_const));

//This will not work
//error C2440: 'const_cast' : cannot convert from 'const int' to 'int'
func(const_cast<int>(My_const));

return 0;
}

Can "const_cast" be used with "int", "long" directly like:
const_cast<int>(My_const))?
Why "func(const_cast<int&>(My_const))" seems like working? What's the
explanation if it's right way to remove constantness from the const int
My_const?

Thanks for your help!
 
R

Ron Natalie

Stub said:
What're the implication of "Count(Count const&)" above? Is it the same as
"Count(const &Count)"? Does it have anything to do with "Count(const Count
&c)"?

Count(const&Count) is not valid syntax.
Count(const Count&) and Count(Count const&) are equivelent. The "const"
commutes with the type name.
Why "func(const_cast<int&>(My_const))" seems like working? What's the
explanation if it's right way to remove constantness from the const int
My_const?

The issue with const_cast<int> is that const cast only converts pointers and
references. You don't even need a cast to convert from const int to int.
 
S

Stub

Ron Natalie said:
Count(const&Count) is not valid syntax.
Count(const Count&) and Count(Count const&) are equivelent. The "const"
commutes with the type name.

So what does Count(const Count&) or Count(Count const&) if defined in the
class Count like this? As a copy constructor? It even doesn't have a
variable of type Count as it's argument - This is very confusing to me.

The issue with const_cast<int> is that const cast only converts pointers and
references. You don't even need a cast to convert from const int to int.
I do need it. Otherwise compile error: error C2664: 'func' : cannot convert
parameter 1 from 'const int' to 'int &'. Any idea why is this?
 
V

Victor Bazarov

Stub said:
same

So what does Count(const Count&) or Count(Count const&) if defined in the
class Count like this? As a copy constructor? It even doesn't have a
variable of type Count as it's argument - This is very confusing to me.

The argument name is irrelevant - it's not used.

However, the issue is that your class (in the original post) has
two copy constructors with precisely the same signature. That is
unacceptable and ought to have been flagged as an error.
I do need it. Otherwise compile error: error C2664: 'func' : cannot convert
parameter 1 from 'const int' to 'int &'. Any idea why is this?

Because the compiler will not itself remove const-ness -- it's not
one of allowed implicit conversions.

You are allowed to do const_cast<int&>(My_const) because in this
case 'My_const' is a reference (of type 'int const&') and all you
do is cast away const-ness.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top