424,279 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,279 IT Pros & Developers. It's quick & easy.

using clone() function

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a

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

P: n/a
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

P: n/a

"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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.