Struct copy inside itself

Started by
9 comments, last by King Mir 10 years, 11 months ago
It looks like you need to think about your problem more, because I cannot see a useful application of what you're trying to do.

That said, the way to create a copy on the heap is this:
struct wheel
{
       wheel *copy;
       wheel() 
       {
              copy = new wheel(*this);
       }
       //also needs copy constructor, assignment, and destructor, to handel freeing correctly.
};

Even better you can use this:
#include<memory>
 
struct wheel
{
       std::shared_ptr<wheel> copy;
       wheel() 
       {
              copy.reset(new wheel(*this));
       }
       //copy constructor, assignment, and destructor not needed; free does not need to be called.
};

This topic is closed to new replies.

Advertisement