Jump to content
  • Advertisement
Sign in to follow this  
CogOfHate

Unity How To Copy Array of Derived Classes Without Pointers

This topic is 1973 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

Hello Everyone,

 

I have a problem I am currently facing and I'm wondering if the community could help me out.  I find it's probably easier to explain with an example.

struct Base
{
};

struct Derived : public Base
{
    Derived(float newX) : x(newX)
    {
    }
    
    float x;
};

struct Unrelated : public Base
{
    Unrelated(char val) : v(val)
    {
    }
    
    char v;
}
...
 
// How to copy the data that's in mBaseArray into another struct?
// Don't want pointer copy as it's not thread safe.
 
// Below is a very simple example.
 
Module1
-----------
class Module1
{
...
    Array<Base*> mBaseArray;
}; 

 

- mBaseArray is populate in some fashion.  Notice the use of polymorphism here.
 
mBaseArray.push_back(new Derived(10));
mBaseArray.push_back(new Unrelated('c'));  
 
... Some sync point here
... After this point, I want to copy the information that is in mBaseArray into Module2's mWorkingArray.
 
Module2
-----------
class Module2
{
...
    Array<Base*> mWorkingArray;
};

 

- Want to copy the data from mBaseArray into Module2's own array.
- Copied data from mBaseArray without the use of pointers.  Want only data.
- Module2 contains a similar declaration to mBaseArray.
 
Is there a way to accomplish this?  Or is there some other method that will solve this problem?
 
If there are better ways of doing what I'm trying to accomplish, I am more than welcome to suggestions.
 
Thanks.

Share this post


Link to post
Share on other sites
Advertisement

If you really need to keep the polymorphism, one option is to add a virtual Clone method to the Base class. Then it will be the responsibility of each subclass to implement this Clone method by creating a new instance of its type and initializing it accordingly. You can then Clone the objects into the new array.

 

Otherwise, revisit whether you actually need polymorphism.

Share this post


Link to post
Share on other sites

Thanks for the suggestion.  I think you were right and I was thinking about stuff in all the wrong way.  I have since moved away from polymorphism in favour of a better approach.  I think I can achieve what I want through proper message passing instead of the convoluted way I was previously using.

Share this post


Link to post
Share on other sites
class Module1
{
...
    Array<Derived> mDerivedArray;
    Array<Unrelated> mUnrelatedArray;
};
mDerivedArray.push_back(Derived(10));
mUnrelatedArray.push_back(Unrelated('c'));
class Module2
{
    Module2(Array<Derived>& d,Array<Unrelated>& u) :
        mDerivedArray(d),
        mUnrelatedArray(u) {}
    Array<Derived>& mDerivedArray;
    Array<Unrelated>& mUnrelatedArray;
};

How about doing it this way? Would be much more efficient and if you dont add a huge class hierarchy it wouldnt hurt.

You can just treat both arrays the same for some types of work if you like. It may be even easier to process the data if you dont throw away the knowledge of the exact type without being forced to and have similar things sorted together for some other types of work.

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!