Connecting Tech Pros Worldwide Forums | Help | Site Map

Is this the correct form of a copy constructor?

Dominique
Guest
 
Posts: n/a
#1: Jul 19 '05
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



lilburne
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Is this the correct form of a copy constructor?


Dominique wrote:
[color=blue][color=green]
>>[/color]
> Is this a problem in the compiler, or have I made an error?
>[/color]

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.

Rob Williscroft
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Is this the correct form of a copy constructor?


Dominique wrote in news:Ynjlb.28629$Ol.566819@read1.cgocable.net:
[color=blue]
> 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()[/color]

Int16 CString::GetLength() const[color=blue]
> {
> ...
> }
>[/color]

change the declaration in your CString class too;

class CString
{
// was Int16 CString::GetLength();
Int16 CString::GetLength() const;
};
[color=blue]
> The error on the marked line is:
>
> const CString as 'this' argument of 'Int16 CString::GetLength()'
> discards qualifiers[/color]

the qualifier here is the const applied to the argument of your
copy-ctor.
[color=blue]
>
> 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?
>[/color]

The compilers right this time.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Mike Wahler
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Is this the correct form of a copy constructor?



"Dominique" <NODOTd.b.g.e.n.e.r.a.l.0.1.LOSEDOTS@cogecoDOTca > wrote in
message news:Ynjlb.28629$Ol.566819@read1.cgocable.net...[color=blue]
> 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[/color]

In this context, the object 'string' is const.
[color=blue]
> ...
> }
>
> and the GetLength member:
>
> Int16 CString::GetLength()
> {
> ...[/color]

In this context, the 'CString' object (*this) is not const.
[color=blue]
> }
>
> The error on the marked line is:
>
> const CString as 'this' argument of 'Int16 CString::GetLength()' discards
> qualifiers[/color]

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.
[color=blue]
> 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.[/color]

That's not where the problem is. :-)
[color=blue]
> Is this a problem in the compiler, or have I made an error?[/color]

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



Mike Wahler
Guest
 
Posts: n/a
#5: Jul 19 '05

re: Is this the correct form of a copy constructor?



"lilburne" <lilburne@godzilla.net> wrote in message
news:bn4gbt$t1er1$1@ID-203936.news.uni-berlin.de...[color=blue]
> Dominique wrote:
>[color=green][color=darkred]
> >>[/color]
> > Is this a problem in the compiler, or have I made an error?
> >[/color]
>
> 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.[/color]

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


David White
Guest
 
Posts: n/a
#6: Jul 19 '05

re: Is this the correct form of a copy constructor?


Mike Wahler <mkwahler@mkwahler.net> wrote in message
news:yBjlb.467$wc3.10@newsread3.news.pas.earthlink .net...[color=blue]
>
> *Any* member function which does not modify the object
> (such as such 'get data' functions) should be defined
> as const.[/color]

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

DW



lilburne
Guest
 
Posts: n/a
#7: Jul 19 '05

re: Is this the correct form of a copy constructor?


Mike Wahler wrote:
[color=blue]
> "lilburne" <lilburne@godzilla.net> wrote in message
> news:bn4gbt$t1er1$1@ID-203936.news.uni-berlin.de...
>[color=green]
>>Dominique wrote:
>>
>>[color=darkred]
>>>Is this a problem in the compiler, or have I made an error?
>>>[/color]
>>
>>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.[/color]
>
>
> 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.
>[/color]

Damn you're right:
http://dotnet.di.unipi.it/Content/ss..._1_1Int16.html

Smalltalk anyone?

Dominique
Guest
 
Posts: n/a
#8: Jul 19 '05

re: Is this the correct form of a copy constructor?


Thanks all, for the help.

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

Dominique


Closed Thread