473,386 Members | 1,795 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,386 software developers and data experts.

copy constructor problem

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?

Mar 29 '06 #1
11 1837
al.c...@gmail.com wrote:
class test {
public:
test()
:a(0)
{

}
int get() { return a; }


Modify the above line so that get() is a const member function
int get() const { return a; }

Mar 29 '06 #2
In message <11**********************@j33g2000cwa.googlegroups .com>,
al*****@gmail.com writes
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; }
--
Richard Herring
Mar 29 '06 #3
al*****@gmail.com wrote:
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
Mar 29 '06 #4
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?

Mar 30 '06 #5
al.cpwn wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 30 '06 #6
In message <11**********************@z34g2000cwc.googlegroups .com>,
al*****@gmail.com 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?

--
Richard Herring
Mar 30 '06 #7
Richard Herring wrote:
In message <11**********************@z34g2000cwc.googlegroups .com>,
al*****@gmail.com 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.
--
Richard Herring


Mar 30 '06 #8
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
Mar 30 '06 #9
Gernot Frisch wrote:
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?


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

Mar 30 '06 #10
In message <11*********************@t31g2000cwb.googlegroups. com>,
al*****@gmail.com writes
Richard Herring wrote:
In message <11**********************@z34g2000cwc.googlegroups .com>,
al*****@gmail.com 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.

--
Richard Herring
Mar 30 '06 #11
In message <P1**************@baesystems.com>, Richard Herring
<ju**@[127.0.0.1]> writes
In message <11*********************@t31g2000cwb.googlegroups. com>,
al*****@gmail.com writes
Richard Herring wrote:
In message <11**********************@z34g2000cwc.googlegroups .com>,
al*****@gmail.com 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.
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.


--
Richard Herring
Mar 30 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
8
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public:...
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
3
by: subramanian | last post by:
Consider the code fragment: class Test { public: Test(const Test &temp); ... }; ....
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
34
by: =?ISO-8859-1?Q?Marcel_M=FCller?= | last post by:
Hi, is there a way to avoid the automatic copy constructor generation. I do not want the object to be non-copyable. I simply do not want that copying is done by the default copy constructor. But...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.