How To Copy Array of Derived Classes Without Pointers

Started by
3 comments, last by King Mir 11 years, 1 month ago

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

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.


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.

On a different note, You should consider using smart pointers.

This topic is closed to new replies.

Advertisement