c++ - Pass instance of base class into derived class -


in application have list of objects of base class type. each 1 of base classes can have multiple configurations (the derived class) , i've been trying figure out how pass base class derived class don't need reinitialize values every time. know can following ways, i'm curious if there's easier/less clunky way since base class in code takes while initialize , has lot of features.

simple example:

class base { public:     base(int a, int b) : a(a), b(b) {} protected:     int a;     int b; };  class derived : public base { public:     derived(int c, int d, base base) : c(c), d(d) {         this->a = base.a;         this->b = base.b;     } private:     int c;     int d; }; 

or (trying avoid due high overhead)

class base { public:     base(int a, int b) : a(a), b(b) {} protected:     int a;     int b; };  class derived : public base { public:     derived(int c, int d, const base &base) : base(base), c(c), d(d) {} private:     int c;     int d; } 

if base has copy constructor can use:

class base { public:     base(int a, int b) : a(a), b(b) {}     base(const base& base) : a(base.a), b(base.b) {} // make own or use default protected:     int a;     int b; };  class derived : public base { public:     derived(int c, int d, const base& base) : base(base), c(c), d(d) {} private:     int c;     int d; } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -