Jump to content

  • Log In with Google      Sign In   
  • Create Account


How To Copy Array of Derived Classes Without Pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 CogOfHate   Members   -  Reputation: 112

Like
0Likes
Like

Posted 21 February 2013 - 09:25 PM

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.


Sponsor:

#2 phil_t   Crossbones+   -  Reputation: 3189

Like
3Likes
Like

Posted 21 February 2013 - 10:49 PM

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.



#3 CogOfHate   Members   -  Reputation: 112

Like
0Likes
Like

Posted 22 February 2013 - 09:46 AM

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.



#4 wintertime   Members   -  Reputation: 1601

Like
0Likes
Like

Posted 23 February 2013 - 06:26 PM

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.



#5 King Mir   Members   -  Reputation: 1909

Like
0Likes
Like

Posted 24 February 2013 - 04:11 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS