Sign in to follow this  

c++ copy all data from parent class?

This topic is 666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

Using c++.

I have

.

class simpleUnit; //base unit
class advUnit: public simpleUnit; //has lots of extra data

.

Now i need to place a copy of all members from simpleUnit (the parent class) INSIDE advUnit, like so:

.

//inside declaration of advUnit
simpleUnit copiedData;

//when i need to copy it
myAdvUnit.copiedData = myAdvUnit.(parentData)

//and later when I need to "restore it"
myAdvUnit.(parentData) = myAdvUnit.copiedData;

.

I would prefer not to copy all members by hand one by one. Any way to do it?

Edited by suliman

Share this post


Link to post
Share on other sites

The standard solution is to use a copy constructor and assignment operator, anything in particular why that doesn't work for you?

 

Note that since "advUnit" class is derived from "simpleUnit" class, each "advUnit" object has its own copy of the simpleUnit data already inside itself, you just need to implement the copy constructor and assignment operator in the base class too, and call them from the derived class, afaik.

Share this post


Link to post
Share on other sites

Is something like this what you want?

I think this will work, assuming B is copyable, but I must admit I've never tried it smile.png

 

class A : public B {

 

  B backup;

}

 

void A:backupB() {

   backup = static_cast<B>(*this);

}

 

void A:restoreB() {

  static_cast<B>(*this) = backup;

}

Edited by Olof Hedman

Share this post


Link to post
Share on other sites
It should work because I have done something similar in the past to bring sanity to a class' horribly broken copy semantics. You don't need the cast in backupB though, object slicing works in your favor there.

Share this post


Link to post
Share on other sites

Well I have loads of members and add them along the way. And I save/load them to/from disk using data range (so i dont need to rewrite those functions when I add/remove members.

 

That way to data from the parent class must be store between two (by me) defined members:

bool startSave;

//whatever is here will be saved/loaded
//so i need to also add parent data

bool endSave;

.

Olof your idea seems to be what I want. I can save the data fine (uu is a advUnit pointer"):

uu->savedData=static_cast<pathUnit>(*uu);

Everything is copied to savedData. Splendid!

But when I try to load it the parentdata is unaffected (nothing seem to happen when I do this. savedData is filled with valid data but not copied back to the parent-part of advUnit):

static_cast<pathUnit>(*uu) = uu->savedData;
Edited by suliman

Share this post


Link to post
Share on other sites

 

Well I have loads of members and add them along the way. And I save/load them to/from disk using data range (so i dont need to rewrite those functions when I add/remove members.

 

That way to data from the parent class must be store between two (by me) defined members:

bool startSave;

//whatever is here will be saved/loaded
//so i need to also add parent data

bool endSave;

.

Olof your idea seems to be what I want. I can save the data fine (uu is a advUnit pointer"):

uu->savedData=static_cast<pathUnit>(*uu);

Everything is copied to savedData. Splendid!

But when I try to load it the parentdata is unaffected (nothing seem to happen when I do this. savedData is filled with valid data but not copied back to the parent-part of advUnit):

static_cast<pathUnit>(*uu) = uu->savedData;

 

You would need to do 

*(static_cast<pathUnit*>(uu)) = uu->savedData;

Share this post


Link to post
Share on other sites

This topic is 666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this