473,406 Members | 2,549 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,406 software developers and data experts.

using clone() function

Hello Experts!

I want to create a virtual clone but I'm not sure if this is the wright way.
Can somebody tell me where I have made error in the code.
I get some compile error in this code also.

Which functions are correct or which have to be modified

class Base
{
public:
virtual Base* clone() = 0;
};

class Sub : public Base
{
public:
virtual Base* clone() = 0;
};

Base* Sub::clone()
{ return new Sub(*this); }

Base* copyObject(Base& baseObject)
{ return baseObject.clone(); }

//Tony

Many thanks!
Aug 22 '05 #1
13 12457
Tony Johansson sade:
Hello Experts!

I want to create a virtual clone but I'm not sure if this is the wright way.
Can somebody tell me where I have made error in the code.
I get some compile error in this code also.

And those errors are?
Which functions are correct or which have to be modified

class Base
{
public:
virtual Base* clone() = 0;
};

class Sub : public Base
{
public:
virtual Base* clone() = 0;
virtual Base * clone();

or

Base * clone();
};

Base* Sub::clone()
{ return new Sub(*this); }

Base* copyObject(Base& baseObject)
{ return baseObject.clone(); }

//Tony

Many thanks!


Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Aug 22 '05 #2

"Tobias Blomkvist" <vo**@void.void> skrev i meddelandet
news:1124704386.64e6f0b347ca9e0f5008c632246aed1d@t eranews...
Tony Johansson sade:
Hello Experts!

I want to create a virtual clone but I'm not sure if this is the wright
way.
Can somebody tell me where I have made error in the code.
I get some compile error in this code also.

And those errors are?
Which functions are correct or which have to be modified

class Base
{
public:
virtual Base* clone() = 0;
};

class Sub : public Base
{
public:
virtual Base* clone() = 0;


virtual Base * clone();

or

Base * clone();
};

Base* Sub::clone()
{ return new Sub(*this); }

Base* copyObject(Base& baseObject)
{ return baseObject.clone(); }

//Tony

Many thanks!

And those errors are?

This is the compile error I get.
c:\Documents and Settings\Tony\kau\cplusplus\test15\base.h(15): error C2143:
syntax error : missing ';' before 'public'

How can main be used with this clone function?

//Tony

Aug 22 '05 #3
class Base
{
public:
virtual Base* clone() = 0;

};

class Sub : public Base
{
public:
virtual Base* clone();/// = 0; this should not be here
//since this will make it a Abstract
//class and so u will not be able to write
// return new Sub(*this);

};

Base* Sub::clone()
{ return new Sub(*this); }

Base* copyObject(Base& baseObject)
{ return baseObject.clone(); }

Aug 22 '05 #4
Tony Johansson sade:

And those errors are?
This is the compile error I get.
c:\Documents and Settings\Tony\kau\cplusplus\test15\base.h(15): error C2143:
syntax error : missing ';' before 'public'


I don't. And I'm guessing you use VC++ since it (some broken version of
it) allows you to give a pure virtual member function a body.

If you include something directly before the class definition the actual
error could be there instead, which propagates causing the syntax error.

How can main be used with this clone function?

int main(int argc, char* argv[])
{
Sub * s = new Sub;
delete copyObject(*s);
delete s;
return 0;
}
//Tony


Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Aug 22 '05 #5

"Tobias Blomkvist" <vo**@void.void> skrev i meddelandet
news:1124706132.2c6fd907fae848b84404faf38ad59909@t eranews...
Tony Johansson sade:

And those errors are?


This is the compile error I get.
c:\Documents and Settings\Tony\kau\cplusplus\test15\base.h(15): error
C2143: syntax error : missing ';' before 'public'


I don't. And I'm guessing you use VC++ since it (some broken version of
it) allows you to give a pure virtual member function a body.

If you include something directly before the class definition the actual
error could be there instead, which propagates causing the syntax error.

How can main be used with this clone function?


int main(int argc, char* argv[])
{
Sub * s = new Sub;
delete copyObject(*s);
delete s;
return 0;
}

By all means this code above must be wrong because you can not delete twice.
You can delete the memory that pointer s is poiting to but what would
delete copyObject(*s); be deleting
Is this below a correct usage of this clone together with a main?
#include "base.h"
int main()
{
Sub* s = new Sub;
Base* b = copyObject(*s);
delete s;
return 0;
}

//Tony
Aug 22 '05 #6
Tobias Blomkvist wrote:
Tony Johansson sade:

And those errors are?

This is the compile error I get.
c:\Documents and Settings\Tony\kau\cplusplus\test15\base.h(15): error
C2143: syntax error : missing ';' before 'public'

I don't. And I'm guessing you use VC++ since it (some broken version of
it) allows you to give a pure virtual member function a body.


It's legal to give a pure virtual member function a body (that can
explicitly be used as a default implementation).

<code>

struct Base
{
virtual void doit() = 0;
};

void Base::doit()
{
// do s'thing here
}

struct Der : Base
{
void doit() { Base::doit(); }
};

</code>

Stefan
Aug 22 '05 #7

"Tony Johansson" <jo*****************@telia.com> skrev i meddelandet
news:l4*******************@newsb.telia.net...

"Tobias Blomkvist" <vo**@void.void> skrev i meddelandet
news:1124706132.2c6fd907fae848b84404faf38ad59909@t eranews...
Tony Johansson sade:


And those errors are?

This is the compile error I get.
c:\Documents and Settings\Tony\kau\cplusplus\test15\base.h(15): error
C2143: syntax error : missing ';' before 'public'


I don't. And I'm guessing you use VC++ since it (some broken version of
it) allows you to give a pure virtual member function a body.

If you include something directly before the class definition the actual
error could be there instead, which propagates causing the syntax error.

How can main be used with this clone function?


int main(int argc, char* argv[])
{
Sub * s = new Sub;
delete copyObject(*s);
delete s;
return 0;
}


I use this IDE .Microsoft Visual C++ .NET 69586-335-0000007-18888
One more thing if I defined this clone method within the class definition I
don't get any compile error
but if I defined it outside the class definition I get the compile error
showed in the beginning of this mail.
This was just for information and not a question
class Sub : public Base //this works fine
{
virtual Base* clone()
{ return new Sub(*this); }
};
Aug 22 '05 #8

Tony Johansson wrote:
Hello Experts!

I want to create a virtual clone but I'm not sure if this is the wright way.
Can somebody tell me where I have made error in the code.
I get some compile error in this code also.

Which functions are correct or which have to be modified

class Base
{
public:
virtual Base* clone() = 0;
};

class Sub : public Base
{
public:
virtual Base* clone() = 0;
};

Base* Sub::clone()
{ return new Sub(*this); }

Base* copyObject(Base& baseObject)
{ return baseObject.clone(); }

//Tony

Many thanks!


Consider using a smart pointer like that in the following link:
http://code.axter.com/clone_ptr.h

With the above clone smart pointer, your objects don't need to have a
clone method. The above smart pointer can correctly clone a derived
type if it's pass to the constructor.
For more info, see following link:
http://code.axter.com/clone_ptr_introduction.htm

Aug 22 '05 #9
In message <1124706132.2c6fd907fae848b84404faf38ad59909@teran ews>,
Tobias Blomkvist <vo**@void.void> writes
Tony Johansson sade:
And those errors are? This is the compile error I get.
c:\Documents and Settings\Tony\kau\cplusplus\test15\base.h(15): error
C2143: syntax error : missing ';' before 'public'


I don't. And I'm guessing you use VC++ since it (some broken version of
it) allows you to give a pure virtual member function a body.


So does the standard.
If you include something directly before the class definition the actual
error could be there instead, which propagates causing the syntax error.
How can main be used with this clone function?


int main(int argc, char* argv[])
{
Sub * s = new Sub;
delete copyObject(*s);
delete s;
return 0;
}
//Tony


Tobias


--
Richard Herring
Aug 22 '05 #10

"Tobias Blomkvist" <vo**@void.void> wrote in message
news:1124706132.2c6fd907fae848b84404faf38ad59909@t eranews...
Tony Johansson sade:

And those errors are?


This is the compile error I get.
c:\Documents and Settings\Tony\kau\cplusplus\test15\base.h(15): error
C2143: syntax error : missing ';' before 'public'


I don't. And I'm guessing you use VC++ since it (some broken version of
it) allows you to give a pure virtual member function a body.

If you include something directly before the class definition the actual
error could be there instead, which propagates causing the syntax error.

How can main be used with this clone function?


int main(int argc, char* argv[])
{
Sub * s = new Sub;
delete copyObject(*s);
delete s;
return 0;
}
//Tony


Tobias


Tobias,

You declared Clone in class Sub as pure virtual with the "=0" at the end of
the declaration. By doing that, you are telling the compiler that it should
be illegal to instantiate an object of type Sub, which is the very thing you
are trying to do in the Clone member function.

Although it was previously pointed out that a pure virtual function can have
a body, which can be called statically (as opposed to dynamically through a
pointer), the presence of a body of a pure virtual function is rarely
necessary. In most cases, a member function is declared pure virtual when
the concept conveyed through the class is so abstract, that it is impossible
to give a solid definition to such a member function. An example would be
the draw function in the abstract class shape. Drawing an abstract shape is
meaningless, in fact.

Ben
Aug 22 '05 #11

ra************@gmail.com wrote:
class Base
{
public:
virtual Base* clone() = 0;

};

class Sub : public Base
{
public:
virtual Base* clone();/// = 0; this should not be here
//since this will make it a Abstract
//class and so u will not be able to write
// return new Sub(*this);

};

Base* Sub::clone()
{ return new Sub(*this); }

Base* copyObject(Base& baseObject)
{ return baseObject.clone(); }


Since the method is called clone() it should return a pointer to the
same type of object as the class for which it is declared:

class Base
{
public:
...
virtual Base * clone() = 0;
};

class Sub : public Base
{
public:
...
virtual Sub * clone();
};

A class that derived from Sub would likewise return a pointer to the
derived class in its clone method.

Greg

Aug 22 '05 #12
Tony Johansson wrote:
How can main be used with this clone function?

int main(int argc, char* argv[])
{
Sub * s = new Sub;
delete copyObject(*s);
delete s;
return 0;
}

By all means this code above must be wrong because you can not delete twice.
You can delete the memory that pointer s is poiting to but what would
delete copyObject(*s); be deleting


copyObject returns a pointer to the newly created object.
Thus the above code is fine.
Is this below a correct usage of this clone together with a main?
#include "base.h"
int main()
{
Sub* s = new Sub;
Base* b = copyObject(*s);
delete s;
return 0;
}


No. You are leaking memory.
copyObject creates a new object and returns a pointer to it.
You never delete this object.

int main()
{
Sub* s = new Sub;
Base* b = copyObject(*s);
delete s;
delete b;
return 0;
}

would be fine.
PS: You need a virtual destructor for this to work correctly.

So better make the base class:

class Base
{
public:
virtual ~Base() {}
virtual Base* clone() = 0;
};

--
Karl Heinz Buchegger
kb******@gascad.at
Aug 22 '05 #13
benben sade:


Tobias,

You declared Clone in class Sub as pure virtual with the "=0" at the end of
the declaration. By doing that, you are telling the compiler that it should
be illegal to instantiate an object of type Sub, which is the very thing you
are trying to do in the Clone member function.
I had already pointed out that myself and corrected the code in my first
reply.

Although it was previously pointed out that a pure virtual function can have
a body, which can be called statically (as opposed to dynamically through a
pointer), the presence of a body of a pure virtual function is rarely
necessary. In most cases, a member function is declared pure virtual when
the concept conveyed through the class is so abstract, that it is impossible
to give a solid definition to such a member function. An example would be
the draw function in the abstract class shape. Drawing an abstract shape is
meaningless, in fact.

Ben


But this is something I completely have missed. My bad.

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Aug 22 '05 #14

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

Similar topics

7
by: johkar | last post by:
I am confused on childNodes or children. When you have nested lists, I would think it would all be one list in the Dom, this doesn't seem to be the case. So how do you traverse an unordered list?...
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
3
by: scoobydoo | last post by:
Hello, I am trying to implement ICloneable's Clone() function, using Serialization. However, my code causes an exception. I have a class derived from TreeNode called "Node1". In Node1, I...
2
by: phx | last post by:
I have to collection that stores the items from the same class. I want to copy the items of the second collection to the first one , and empty the second. first.clean() first=second second() ...
2
by: Rob R. Ainscough | last post by:
Is there an easy way to clone an object? What I've done in the past is write my own Clone method for each class i create and then just assign property values across objects. I was hoping VS...
8
by: Noozer | last post by:
I'm looking for a way to generate a "clone" of an object. Right now I need to write a Clone function for every class that make and I'd like to have a generic routine. Instead of doing this: For...
2
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is...
5
by: Raman | last post by:
Hello friends, I want to print an ID card. I have one Windows Form that contains front and back side. The printer is printing both front and back side at a time. I am trying to send both sides...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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...

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.