C++ - How to update pointer (or members) between instances of same class -



have simple class consists of void pointer , int (this sort of boost::variant educational project).
have working copy constructor , destructor.

but grinds gears is, how accomplish this:

container cont1("some value"); //simple construction container cont2; cont2.createlink(cont1); //this should initialize members reference (or alike) std::cout<<cont1; //yields "some value"  cont2.set(20); //setting container should update original container too, since initialized reference (or smth alike) std::cout<<cont1; //yields 20 

this simplified version of class:

class container {     public:         container(){}         container(const std::string &val){var.type = string; var.data = new std::string(val);}         container(int val){ /* same int */}         container(const container &val){ /* memory copy */}          void set(int val){ /* set value if type matches, otherwise allocate new pointer */}         void set(const std::string &val){ /* same int */}         void createlink(const container &val){ /* somehow assign reference or whatsoever */}     private:         typedef struct var {             int type = 0;             void *data = null; }                var var; } 

if set value of cont2 string (i.e. same data type holds @ moment), fine, because set not allocate new pointer , rather assign new value.
how make sure pointer of cont1 updates if assign different value cont2 , therefore have allocate new pointer?

would need shared_pointer?

thanks insight!

edit:

i changed function name make more clear should happen.

there solution involves straight oo. create interface variant type, , use double indirection variant instance allow linked containers share same variant instance.

the reason double indirection required because of way want set() method automatically allocate new variant instance if new type doesn't match original type. if shared pointer variant both containers, after set() creates new variant instance, each container referring different instances again.

to around that, can use pointer pointer variant in container instead.

here possible way define variant interface, , how subclassed:

typedef std::ostream out; struct badtype {};  struct var {     virtual ~var () = default;     virtual out & print (out &os) { return os << "(badtype)"; }     virtual void set (int) { throw badtype(); }     virtual void set (const std::string &) { throw badtype(); } };  struct varinteger : var {     int data;     varinteger (int v) : data(v) {}     out & print (out &os) { return os << data; }     void set (int v) throw() { data = v; } };  struct varstring : var {     std::string data;     varstring (const std::string &v) : data(v) {}     out & print (out &os) { return os << data; }     void set (const std::string &v) throw() { data = v; } }; 

here how define pointer pointer, , how initialized:

typedef std::shared_ptr<var> varptr; std::shared_ptr<varptr> varptr_;  static varptr make_var () { return std::make_shared<var>(); } static varptr make_var (int v) { return std::make_shared<varinteger>(v); } static varptr make_var (const std::string &v) {     return std::make_shared<varstring>(v); }  varptr & var () { return *varptr_; } const varptr & var () const { return *varptr_; }  container () : varptr_(std::make_shared<varptr>(make_var())) {} container (int v) : varptr_(std::make_shared<varptr>(make_var(v))) {} container (const std::string &v)     : varptr_(std::make_shared<varptr>(make_var(v))) {} 

and here how set() methods , createlink() method implemented.

void set (int v) {     try { var()->set(v); }     catch (badtype) { var() = make_var(v); } }  void set (const std::string &v) {     try { var()->set(v); }     catch (badtype) { var() = make_var(v); } }  void createlink (const container &val) { varptr_ = val.varptr_; } 

demo


Comments