c++ copy all data from parent class?

Started by
7 comments, last by suliman 8 years, 2 months ago

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?

Advertisement

Do you mean something like this?


simpleUnit Backup;
myAdvUnit.BackupUnit(Backup);

//.. some time later

myAdvUnit.RestoreUnit(Backup);

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.

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;

}

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

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;

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;

Wohoo! Works!

Thank you. The syntax gets messy in these cases:)

This topic is closed to new replies.

Advertisement