copy constructor problem

A

al.cpwn

class test {
public:
test()
:a(0)
{

}
int get() { return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a++;
}
private:
int a;
};


Intuitively it seems that the parameter to copy constructor should be
const test&
However, if I try to use get() method, my compiler gives the error:

i:\Documents and Settings\Al Cpwn\My Documents\Visual Studio
Projects\c++\1.cpp(11) : error C2662: 'test::get' : cannot convert
'this' pointer from 'const test' to 'test &'

Can someone please help me understand why?
 
R

Richard Herring

class test {
public:
test()
:a(0)
{

}
int get() { return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a++;
}
private:
int a;
};


Intuitively it seems that the parameter to copy constructor should be
const test&
However, if I try to use get() method, my compiler gives the error:

i:\Documents and Settings\Al Cpwn\My Documents\Visual Studio
Projects\c++\1.cpp(11) : error C2662: 'test::get' : cannot convert
'this' pointer from 'const test' to 'test &'

Can someone please help me understand why?

You're trying to call the non-const function get() on the const
reference argument t.

Change it to int get() const { return a; }
 
K

Kai-Uwe Bux

class test {
public:
test()
:a(0)
{

}
int get() { return a; }
Try:

int get () const { return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a++;
}
private:
int a;
};


Intuitively it seems that the parameter to copy constructor should be
const test&
However, if I try to use get() method, my compiler gives the error:


Best

Kai-Uwe Bux
 
A

al.cpwn

I have a question: how does the compiler know which function to call,
is the const keyword sufficient? For primitive types such as int or
double, do we also have const keyword specified for certain operations?
 
P

Phlip

al.cpwn said:
I have a question: how does the compiler know which function to call,
is the const keyword sufficient? For primitive types such as int or
double, do we also have const keyword specified for certain operations?

Could you post a code sample of the effect you are asking about?

The guideline is "top-level const is bad karma", but we don't know if you
are asking about top-level const.
 
R

Richard Herring

In message <[email protected]>,
(e-mail address removed) writes

[please quote some context so we know what you're talking about]
I have a question: how does the compiler know which function to call,
is the const keyword sufficient?

If a class has both const and non-const versions of a member function,
it decides which to call on the basis of whether the object (or the
salient reference or pointer to it) is const or not.
For primitive types such as int or
double, do we also have const keyword specified for certain operations?
Primitive types don't have such member functions. Can you give an
example of what you mean?
 
A

al.cpwn

Richard said:
In message <[email protected]>,
(e-mail address removed) writes

[please quote some context so we know what you're talking about]
I have a question: how does the compiler know which function to call,
is the const keyword sufficient?

If a class has both const and non-const versions of a member function,
it decides which to call on the basis of whether the object (or the
salient reference or pointer to it) is const or not.
For primitive types such as int or
double, do we also have const keyword specified for certain operations?
Primitive types don't have such member functions. Can you give an
example of what you mean?
for example:
int a=0;
a++; //++(int) is non const

const int b=0;
b++; //error

do we get an error because there is no const function named ++(int)? or
do we get an error because the compiler simply knows that ++(int) will
change b somehow.
 
G

Gernot Frisch

do we get an error because there is no const function named ++(int)?
or
do we get an error because the compiler simply knows that ++(int)
will
change b somehow.

Don't you mate aliens because you know it won't bring forth kids, or
do you not mate them because they don't exist?
What's the difference?

-Gernot
 
A

al.cpwn

Gernot said:
Don't you mate aliens because you know it won't bring forth kids, or
do you not mate them because they don't exist?
What's the difference?

thank you for taking the time to share your rhetoric. If you had a
better response that would have been great as well.
 
R

Richard Herring

Richard said:
In message <[email protected]>,
(e-mail address removed) writes

[please quote some context so we know what you're talking about]
I have a question: how does the compiler know which function to call,
is the const keyword sufficient?

If a class has both const and non-const versions of a member function,
it decides which to call on the basis of whether the object (or the
salient reference or pointer to it) is const or not.
For primitive types such as int or
double, do we also have const keyword specified for certain operations?
Primitive types don't have such member functions. Can you give an
example of what you mean?
for example:
int a=0;
a++; //++(int) is non const

const int b=0;
b++; //error

do we get an error because there is no const function named ++(int)?

Yes. According to section 13.6 the built-in operator ++ is equivalent to
calling the (non-member) operators operator++(int &) {prefix} or
operator++(int &, int) {postfix} and there are no equivalent operators
taking const int & arguments. So technically the error is indeed because
there is no const operator++ that can be applied to int.
or
do we get an error because the compiler simply knows that ++(int) will
change b somehow.
It amounts to the same thing. Since you can't redefine the built-in
operators for arithmetic types, there's no way to tell the difference.
 
R

Richard Herring

Richard Herring said:
Richard said:
In message <[email protected]>,
(e-mail address removed) writes

[please quote some context so we know what you're talking about]

I have a question: how does the compiler know which function to call,
is the const keyword sufficient?

If a class has both const and non-const versions of a member function,
it decides which to call on the basis of whether the object (or the
salient reference or pointer to it) is const or not.

For primitive types such as int or
double, do we also have const keyword specified for certain operations?

Primitive types don't have such member functions. Can you give an
example of what you mean?
for example:
int a=0;
a++; //++(int) is non const

const int b=0;
b++; //error

do we get an error because there is no const function named ++(int)?

Yes. According to section 13.6 the built-in operator ++ is equivalent
to calling the (non-member) operators operator++(int &) {prefix} or
operator++(int &, int) {postfix} and there are no equivalent operators
taking const int & arguments. So technically the error is indeed
because there is no const operator++ that can be applied to int.

Oops... because there is no operator++ that can be applied to const int
&.
It's a non-member, so the constness of the operator itself is moot.
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top