assignment operator for an ABC

B

bytebro

I normally use something like this for an assignment operator when
class Thing has a smart pointer p_ to private implementation data:

Thing&
Thing::eek:perator=(const Thing& rhs)
{
if (this != &rhs)
{
Thing temp(rhs);
swap(p_, temp.p_);
}
return *this;
}

My problem is that this fails to compile is any member functions in
Thing are pure.

thing.cc: In member function 'Thing& Thing::eek:perator=(const Thing&)':
thing.cc:56: error: cannot declare variable 'temp' to be of abstract
type 'Thing'

Is there some standard way around this?
 
K

Kai-Uwe Bux

bytebro said:
I normally use something like this for an assignment operator when
class Thing has a smart pointer p_ to private implementation data:

Thing&
Thing::eek:perator=(const Thing& rhs)
{
if (this != &rhs)
{
Thing temp(rhs);
swap(p_, temp.p_);
}
return *this;
}

My problem is that this fails to compile is any member functions in
Thing are pure.

thing.cc: In member function 'Thing& Thing::eek:perator=(const Thing&)':
thing.cc:56: error: cannot declare variable 'temp' to be of abstract
type 'Thing'

Is there some standard way around this?

First, you should ask yourself whether you really _want_ an assignment
operator in your base class. Note that such a gadget would allow you to
cross-assign from different derived classes:

class SomeThing : public Thing {...};
class SomethingElse : public Thing {...};

SomeThing some_thing;
SomeThingElse something_else;

Would it make sense to be able to do

some_thing = something_else;


If you find yourself to have a need for run-time polymorphism of value
objects (as opposed to entity objects, for which assignment is not really
all that meaningfull), you might want to have a look into duck-typing and
how it can be implemented in C++. You can use tr1::function as a model.


Best

Kai-Uwe Bux
 
B

bytebro

First, you should ask yourself whether you really _want_ an assignment
operator in your base class. Note that such a gadget would allow you to
cross-assign from different derived classes:

Hmmm. My base class implements a bunch of common functionality, some
of which requires information about the derived class. For example,
an abstract block cipher can always encrypt a block with a key, but
the block-size and a max-key-size may differ for specific block
ciphers. I currently have the derived class stash it's 'personal'
info into memvars of the base class, so that the derived class need
know nothing about the stuff implemented by the base class which,
apart from these 'personal' data, is identical for all derived
classes.

I guess I need to re-think how I'm designing this.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top