473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Questions of reference

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
3 3832

"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Stockwell | last post by:
Hi, Another of my crazy questions. I'm just in the process of learning so bear with me if you can. I actually ran it.... with two test cases TEST CASE 1: Say I have the following defined:...
2
by: Ross Micheals | last post by:
All I have some general .NET questions that I'm looking for some help with. Some of these questions (like the first) are ones that I've seen various conflicting information on, or questions that...
4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
27
by: Jatinder | last post by:
I 'm a professional looking for the job.In interview these questions were asked with some others which I answered.But some of them left unanswered.Plz help. Here are some questions on C/C++, OS...
15
by: Pelle Beckman | last post by:
Hi all, I have a few newbie questions: In function declaration what does a 'const' mean inside the parameter list ? That it won't modify the value? void MemberFunction(const int x);
0
by: Michael.Suarez | last post by:
So we develop and maintain several applications used by several people in the same company, on the same intranet. There are several applications written in VB6, but going forward all of the new...
12
by: Gilbert | last post by:
H, i'm starting with asp.net/vb.net and have some questions about arrays and collections: 1) what's the difference between: dim x() as string and dim x as array
10
by: weird0 | last post by:
I heard of two c# interview questions that i still clearly don't know and havent understood the concept. One reason is also that I havent worked upon them 1. What is the difference between a...
5
by: rhino | last post by:
I was asking questions about the DB2 certification exams a couple of weeks ago and I realize I forgot to ask two questions. I suspect I know the answers but it would be very helpful to get...
10
by: anil reddy | last post by:
Dotnet Interview questions and FAQs, Jobs. Please give your feed back http://interviews.dotnetthread.com
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.