473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Copy constructor problems

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
Jul 19 '05 #1
6 3527

"Ted" <te******@bigfoot.com> wrote in message
news:24**************************@posting.google.c om...
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
Jul 19 '05 #2


Ted wrote:
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?

Jul 19 '05 #3

"Christian Jaeger" <cj@sim.bepr.ethz.ch> wrote in message news:3f********@pfaff2.ethz.ch...
class test
{
public:
test();
test(const test & x);
test(test &x, char *szfile="test.dat");

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.
Jul 19 '05 #4

"Christian Jaeger" <cj@sim.bepr.ethz.ch> wrote in message
news:3f********@pfaff2.ethz.ch...


Ted wrote:
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?


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

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m...


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.


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


Jul 19 '05 #6

"Howard" <al*****@hotmail.com> wrote in message news:bk********@dispatch.concentric.net...
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.
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
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...
7
by: madhukar_bm | last post by:
We know that copy constructor is used if a class object is passed by value to a function, is there any logic behind the use of copy constructor as assignment operator could also have been used...
8
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:...
7
by: skishorev | last post by:
Hi, I know What is the copy constructor. I don't know where and why we have to use copy constructor. If You know please give to me with a situation where we have to use or with a small example....
4
by: john | last post by:
Hey guys, Thank you for your input from the last topic. I tried referencing my c+ + book and googled the topic but i'am still confused a bit. I have another problem with that same code. I was...
13
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
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
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...
10
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.