Copy constructor problems

T

Ted

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
 
M

Mike Wahler

Ted said:
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

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

Sounds reasonable to me.
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);

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

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
 
C

Christian Jaeger

Ted said:
I have the following code:

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

But then, if later you say

test MyTest(OtherTest);

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

Ron Natalie

test MyTest(OtherTest);

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

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.
 
J

jeffc

Christian Jaeger said:
But then, if later you say

test MyTest(OtherTest);

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

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

Howard

Ron Natalie said:
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.

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
 
R

Ron Natalie

Howard said:
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!

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.
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top