C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Copy constructor not called in a clone function of a templat

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Charles-Antoine Giuliani
Guest





PostPosted: Sun Jan 30, 2005 11:52 pm    Post subject: Copy constructor not called in a clone function of a templat Reply with quote



Hello to all!

First I would like to thank all of you for the support you provide ;-)

I am trying to write an adapter for some classes, and among other
things, give them a clone function. However the clone function does
not call the copy constructor but a default constructor which I did
not generate. The template adapter and interface look like this:

class TSDateInterface {
public:
virtual ~TSDateInterface(void) {};
virtual TSDateInterface* Clone(void) const = 0;
virtual std::string Str(void) const = 0;
};

template <typename T> class TSDateStandardAdapter : public
TSDateInterface {
public:
TSDateStandardAdapter(void) : storedDate_(NULL) {};
TSDateStandardAdapter& operator = (const
TSDateStandardAdapter& other) {
TSDateStandardAdapter ake(other);
Swap(ake, *this);
};
TSDateStandardAdapter(const T& toT) : storedDate_(new T(toT))
{};
virtual ~TSDateStandardAdapter(void)
{delete storedDate_;};
virtual TSDateStandardAdapter* Clone(void) const
{ return new TSDateStandardAdapter(*this);};
virtual std::string Str(void) const
{ return storedDate_->Str(); };
T* storedDate_;
private:
void Swap(TSDateStandardAdapter& other) {
T* temp = other.storedDate_;
other.storedDate_ = storedDate_;
storedDate_ = temp;
};
};

However when I compile this code and make the following assignment :
TSDateStandardAdapter<TSDateImplYear>* zz(new

TSDateStandardAdapter<TSDateImplYear>(TSDateImplYear(2001)));
cout << zz->Str() << ' ';
TSDateInterface* zz2 = zz->Clone();
cout << zz2->Str() << ' ';
delete zz;
cout << zz2->Str() << ' ';

The last zz2->Str() gives me an invalid value.

When debugging the code, the clone function does not call the copy
constructor from TSDateAdapter but another generated constructor. (I
am using visual c++ 7.1)

What am I doing wrong ?

Thanks in advance,

Charles


For reference, class TSDateImplYear is something like that:

class TSDateImplYear {
public:
TSDateImplYear (ticktype toy=
std::numeric_limits<ticktype>::min()) : year_(toy)
{ };
std::string Str(void) const {
std::ostringstream str;
str << year_;
return (str.str());
};
private:
ticktype year_;
};


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Maxim Yegorushkin
Guest





PostPosted: Mon Jan 31, 2005 11:43 am    Post subject: Re: Copy constructor not called in a clone function of a tem Reply with quote



On 30 Jan 2005 18:52:28 -0500, Charles-Antoine Giuliani
<apprentice_99 (AT) hotmail (DOT) com> wrote:

Quote:
am trying to write an adapter for some classes, and among other
things, give them a clone function. However the clone function does
not call the copy constructor but a default constructor which I did
not generate. The template adapter and interface look like this:
class TSDateInterface {
public:
virtual ~TSDateInterface(void) {};
virtual TSDateInterface* Clone(void) const = 0;
virtual std::string Str(void) const = 0;
};
template <typename T> class TSDateStandardAdapter : public
TSDateInterface {
public:
TSDateStandardAdapter(void) : storedDate_(NULL) {};
TSDateStandardAdapter& operator = (const
TSDateStandardAdapter& other) {
TSDateStandardAdapter ake(other);
Swap(ake, *this);
};
TSDateStandardAdapter(const T& toT) : storedDate_(new T(toT))
{};
virtual ~TSDateStandardAdapter(void)
{delete storedDate_;};
virtual TSDateStandardAdapter* Clone(void) const
{ return new TSDateStandardAdapter(*this);};
virtual std::string Str(void) const
{ return storedDate_->Str(); };
T* storedDate_;
private:
void Swap(TSDateStandardAdapter& other) {
T* temp = other.storedDate_;
other.storedDate_ = storedDate_;
storedDate_ = temp;
};
};

You have not declared TSDateStandardAdapter's copy constructor, you've
only got a default constructor, an assignment operator (which is not the
same as a copy constructor) and a conversion constructor taking T. Just
declare the copy constructor TSDateStandardAdapter(TSDateStandardAdapter
const&);

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
B. Perlman
Guest





PostPosted: Mon Jan 31, 2005 8:12 pm    Post subject: Re: Copy constructor not called in a clone function of a tem Reply with quote



Hi. I will include just the constructors in your class, as you posted
it, with annotations:
default constructor:
Quote:
TSDateStandardAdapter(void) : storedDate_(NULL) {};
constructor taking an argument of the template type:
TSDateStandardAdapter(const T& toT) : storedDate_(new T(toT))
{};

I don't see any other constructors in the class definition. Where is
your copy constructor?
Quote:

...
When debugging the code, the clone function does not call the copy
constructor from TSDateAdapter but another generated constructor. (I
am using visual c++ 7.1)

What am I doing wrong ?

As far as I can tell, you have no copy constructor. Thus, the clone
function can not call it. In the absence of a copy constructor, the
compiler generated one which is a dumb copy and not what you want.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Charles-Antoine Giuliani
Guest





PostPosted: Tue Feb 01, 2005 12:46 am    Post subject: Re: Copy constructor not called in a clone function of a tem Reply with quote

Thanks a lot. This was pretty stupid from me, I feel shameful Wink I
tried to debug this code for 3 hours, absolutely convinced I had
written a copy constructor, whereas I had a conversion constructor...
Now it works fine ;-)

Charles

On 31 Jan 2005 15:12:36 -0500, "B. Perlman" <batya (AT) excalibur (DOT) co.il>
wrote:

Quote:
Hi. I will include just the constructors in your class, as you posted
it, with annotations:
default constructor:
TSDateStandardAdapter(void) : storedDate_(NULL) {};
constructor taking an argument of the template type:
TSDateStandardAdapter(const T& toT) : storedDate_(new T(toT))
{};

I don't see any other constructors in the class definition. Where is
your copy constructor?

...
When debugging the code, the clone function does not call the copy
constructor from TSDateAdapter but another generated constructor. (I
am using visual c++ 7.1)

What am I doing wrong ?

As far as I can tell, you have no copy constructor. Thus, the clone
function can not call it. In the absence of a copy constructor, the
compiler generated one which is a dumb copy and not what you want.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.