By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,226 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,226 IT Pros & Developers. It's quick & easy.

Questions of reference

P: n/a
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!

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Stub" <st**@asof.com> wrote in message news:_j*********************@bgtnsc05-news.ops.worldnet.att.net...
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.
Jul 19 '05 #2

P: n/a
Ron Natalie <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"Stub" <st**@asof.com> wrote in message news:_j*********************@bgtnsc05-news.ops.worldnet.att.net...
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.


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.

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.

I do need it. Otherwise compile error: error C2664: 'func' : cannot convert
parameter 1 from 'const int' to 'int &'. Any idea why is this?
Jul 19 '05 #3

P: n/a
"Stub" <st**@asof.com> wrote...
Ron Natalie <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

"Stub" <st**@asof.com> wrote in message news:_j*********************@bgtnsc05-news.ops.worldnet.att.net...
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.


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.
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.

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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.