473,503 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1841
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
5720
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
3289
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
2525
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
1757
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
2363
by: subramanian | last post by:
Consider the code fragment: class Test { public: Test(const Test &temp); ... }; ....
13
2438
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
3587
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
3934
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
987
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
3637
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
7199
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
7076
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
7323
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...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5576
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,...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.