• 10
• 13
• 19
• 27
• 9

# c++ copy all data from parent class?

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

## 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

//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 on other sites

Do you mean something like this?

simpleUnit Backup;

//.. some time later



##### 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 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

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 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 on other sites
Could the OP give some more detail of what they are trying to achieve here? Something is badly wrong or maybe misunderstood about how C+++ works here I think.

##### 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 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;