Is this the correct form of a copy constructor?

D

Dominique

Hi

Can anyone help here. I have defined a copy constructor:

CString::CString (const CString &string) // copy constructor
{
Int16 size = string.GetLength() + 1; // this line generates error
....
}

and the GetLength member:

Int16 CString::GetLength()
{
....
}

The error on the marked line is:

const CString as 'this' argument of 'Int16 CString::GetLength()' discards
qualifiers

I am using the GCC compiler, and I assume the 'const' is the source of the
problem, but everywhere I look, the copy constructor seems to be defined
correctly.

Is this a problem in the compiler, or have I made an error?

Thanks
Dominique
 
L

lilburne

Dominique said:
Is this a problem in the compiler, or have I made an error?

Yes you have an error! You are calling a non-const method on
a const object 'string.GetLength()'.

BTW: love the cutesy redefinition of short.
 
R

Rob Williscroft

Dominique wrote in
Hi

Can anyone help here. I have defined a copy constructor:

CString::CString (const CString &string) // copy constructor
{
Int16 size = string.GetLength() + 1; // this line generates error
...
}

and the GetLength member:

Int16 CString::GetLength()

Int16 CString::GetLength() const

change the declaration in your CString class too;

class CString
{
// was Int16 CString::GetLength();
Int16 CString::GetLength() const;
};
The error on the marked line is:

const CString as 'this' argument of 'Int16 CString::GetLength()'
discards qualifiers

the qualifier here is the const applied to the argument of your
copy-ctor.
I am using the GCC compiler, and I assume the 'const' is the source of
the problem, but everywhere I look, the copy constructor seems to be
defined correctly.

Is this a problem in the compiler, or have I made an error?

The compilers right this time.

HTH

Rob.
 
M

Mike Wahler

Dominique said:
Hi

Can anyone help here. I have defined a copy constructor:

CString::CString (const CString &string) // copy constructor
{
Int16 size = string.GetLength() + 1; // this line generates error

In this context, the object 'string' is const.
...
}

and the GetLength member:

Int16 CString::GetLength()
{
...

In this context, the 'CString' object (*this) is not const.
}

The error on the marked line is:

const CString as 'this' argument of 'Int16 CString::GetLength()' discards
qualifiers

It's essentially telling you that you're passing a const
object ('string') to a nonconst member function, which will
treat it (via the nonconst '*this') as a nonconst object,
by 'discarding the const qualifier'. You have just thrown
away the protection against inadvertent modification which
the copy ctor argument type originally provided.
I am using the GCC compiler, and I assume the 'const' is the source of the
problem, but everywhere I look, the copy constructor seems to be defined
correctly.

That's not where the problem is. :)
Is this a problem in the compiler, or have I made an error?

You need to define 'GetLength()' as a const member function:

Int16 CString::GetLength() const
{
/* etc */
};

And don't forget to change the prototype to match.

*Any* member function which does not modify the object
(such as such 'get data' functions) should be defined
as const.

When I first came to C++, I found these 'const' issues
to be somewhat confusing and 'annoying', but quickly
came to see them for the lifesavers they really are. :)

HTH,
-Mike
 
M

Mike Wahler

lilburne said:
Yes you have an error! You are calling a non-const method on
a const object 'string.GetLength()'.

BTW: love the cutesy redefinition of short.

How do you know it's a typedef for short?
It might be e.g. an 'int', or perhaps could even
be some complex class in its own right.

-Mike
 
D

David White

Mike Wahler said:
*Any* member function which does not modify the object
(such as such 'get data' functions) should be defined
as const.

Depending on what exactly you mean by "does not modify the object".

DW
 
D

Dominique

Thanks all, for the help.

BTW, the Int16 is a 'Palm OS type', typedef'd from a 'signed short'!

Dominique
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top