Jump to content
  • Advertisement
Sign in to follow this  
suliman

c++ copy all data from parent class?

This topic is 969 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
Advertisement

Do you mean something like this?

simpleUnit Backup;
myAdvUnit.BackupUnit(Backup);

//.. some time later

myAdvUnit.RestoreUnit(Backup);

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
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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!