Connecting Tech Pros Worldwide Forums | Help | Site Map

Copy constructor problems

Ted
Guest
 
Posts: n/a
#1: Jul 19 '05
I have the following code:

class test
{
public:
test();
test(const test & x);
test(test &x, char *szfile="test.dat");
}

I am using Visual C++ Net 2003 to compile the code. I get the
following warning message:
warning C4521: 'test' : multiple copy constructors specified

This message indicates that it will use the first copy constructor and
ignore the rest.

I found that if I don't specify a default value for szfile then the
compiler doesn't generate the warning, i.e. test(test &x, char
*szfile);

Is there anyway to get around this problem? I still want to specify a
default value for szfile.

Thanks,
Ted

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

re: Copy constructor problems



"Ted" <tedcamp1@bigfoot.com> wrote in message
news:24150ade.0309250743.67326862@posting.google.c om...[color=blue]
> I have the following code:
>
> class test
> {
> public:
> test();
> test(const test & x);
> test(test &x, char *szfile="test.dat");
> }
>
> I am using Visual C++ Net 2003 to compile the code. I get the
> following warning message:
> warning C4521: 'test' : multiple copy constructors specified[/color]

Yes, any ctor which can accept a reference to its
class as it's only argument when being called,
is a copy ctor.
[color=blue]
>
> This message indicates that it will use the first copy constructor and
> ignore the rest.[/color]

Sounds reasonable to me.
[color=blue]
>
> I found that if I don't specify a default value for szfile then the
> compiler doesn't generate the warning, i.e. test(test &x, char
> *szfile);[/color]

Right, now a second argument is needed to call it.
[color=blue]
>
> Is there anyway to get around this problem? I still want to specify a
> default value for szfile.[/color]

You can't and not call it a copy ctor.

What specifically are you trying to do? Your 'filename'
argument implies that you're going to use something in
the file to construct the new object. But that's not
what a copy ctor is for, it's for making an (exact)
copy of an existing object. All the info you need for
this is available from a reference to the object's type.

-Mike


Christian Jaeger
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Copy constructor problems




Ted wrote:[color=blue]
> I have the following code:
>
> class test
> {
> public:
> test();
> test(const test & x);
> test(test &x, char *szfile="test.dat");
> };[/color]

But then, if later you say

test MyTest(OtherTest);

How should the compiler guess, which of the two ctors you want?

Ron Natalie
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Copy constructor problems



"Christian Jaeger" <cj@sim.bepr.ethz.ch> wrote in message news:3f7316a3$1@pfaff2.ethz.ch...
[color=blue][color=green]
> > class test
> > {
> > public:
> > test();
> > test(const test & x);
> > test(test &x, char *szfile="test.dat");[/color][/color]
[color=blue]
>
> test MyTest(OtherTest);
>
> How should the compiler guess, which of the two ctors you want?[/color]

That's not ambiguous. If OtherTest is const, then test(const test&)
is called. If OtherTest is not const, then test(text&, char*) is called.
The warning is just a warning. The program is well-formed.


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

re: Copy constructor problems



"Christian Jaeger" <cj@sim.bepr.ethz.ch> wrote in message
news:3f7316a3$1@pfaff2.ethz.ch...[color=blue]
>
>
> Ted wrote:[color=green]
> > I have the following code:
> >
> > class test
> > {
> > public:
> > test();
> > test(const test & x);
> > test(test &x, char *szfile="test.dat");
> > };[/color]
>
> But then, if later you say
>
> test MyTest(OtherTest);
>
> How should the compiler guess, which of the two ctors you want?[/color]

But if the compiler couldn't guess, then it would have issued an error, not
merely a warning.


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

re: Copy constructor problems



"Ron Natalie" <ron@sensor.com> wrote in message
news:3f7318fe$0$82074$9a6e19ea@news.newshosting.co m...[color=blue]
>
>[color=green]
> >
> > test MyTest(OtherTest);
> >
> > How should the compiler guess, which of the two ctors you want?[/color]
>
> That's not ambiguous. If OtherTest is const, then test(const test&)
> is called. If OtherTest is not const, then test(text&, char*) is[/color]
called.[color=blue]
> The warning is just a warning. The program is well-formed.
>
>[/color]

If that's so, then why did the OP say that his compiler told him it would
ignore the second copy-constructor? I belive you, by the way, but I'm
wondering why his compiler doesn't...? My compiler (CodeWarrior) doesn't
even give a warning!

-Howard




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

re: Copy constructor problems



"Howard" <alicebt@hotmail.com> wrote in message news:bkvk2t$j4g@dispatch.concentric.net...
[color=blue]
> If that's so, then why did the OP say that his compiler told him it would
> ignore the second copy-constructor? I belive you, by the way, but I'm
> wondering why his compiler doesn't...? My compiler (CodeWarrior) doesn't
> even give a warning![/color]

I can tell you for sure it is not ambiguous. As to what his compiler tells him,
I have no clue. G++ 3.2.2 does NOT flag this line (even with -Wall). It
does properly select the constructor based on the const-ness of the copied
value.


Closed Thread