Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable constructors C++

One of the corner stones of OOP is reusing code instead of repeat it over and over. Thus, your projects shorten and get more readable.

C++ gives you all the tools you need to reuse methods instead of repeating the code. Although when it comes to constructors I do not know how to reuse them.

I am not talking of heritage or how to send a message to the father. I am talking about reusing the constructor of the class itself.

The analogy in JAVA is something like this:

public Foo() {
    this(0,0,0);//Not needed in this case, just to clarify
}

public Foo(Foo f){
    this(f.getA(), f.getB(), f.getC());
}

public Foo(int a, int b, int c) {
    this.a = a;
    this.b = b;
    this.c = c;
}

My question is, is there any syntaxis in C++ that allows you to do so?

like image 938
eversor Avatar asked Oct 05 '11 21:10

eversor


3 Answers

C++11 has added constructor delegation and constructor inheritance.

To inherit constructors, a using-declaration is required:

class Base { ... };

class Derived : public Base
{
    using Base::Base;
};

To delegate, use the ctor-initializer, but specify another constructor in the same class, instead of any subobjects (all base and member subobjects will be initialized by the constructor delegated to):

class Another : public Base
{
    int member;
    Another(int x)
        : Base(), member(x) // non-delegating constructor initializes sub-objects
    {}


    Another(void)
        : Another(5) // delegates -- other constructor takes care of Base and member
    {}
};

And perfect forwarding can also come in handy.

like image 72
Ben Voigt Avatar answered Oct 23 '22 23:10

Ben Voigt


Others already answered about C++11, but for C++03 there's a possible workaround: using a base class with needed constructor(s).

struct foo_base {
    foo_base(int a, int b, int c) : a(a), b(b), c(c) { }
    int a, b, c;
};

struct foo : foo_base {
    foo() : foo_base(0, 0, 0) { }
    foo(const foo& other) : foo_base(other.a, other.b, other.c) { }
    foo(int a, int b, int c) : foo_base(a, b, c) { }
};

Of course, you need to consider whether it's worth the boilerplate for your purposes.

like image 20
Cat Plus Plus Avatar answered Oct 23 '22 23:10

Cat Plus Plus


The generally accepted soultion for current compilers is to do this:

class Bar{
pubilc:    
Foo() {
   init(0,0,0);
}

Foo(const Foo &f){
  init(f.getA(), f.getB(), f.getC());
}

Foo(int a, int b, int c) {
  init(a,b,c);
}

private:

void init(int a, int b, int c){
  this->a = a;
  this->b = b;
  this->c = c;
}
};

While this may seem like over kill in this example, that is only because of the simplicity of the example. In a real world application this would actually bring benefits in terms of reduction of repeated code.

like image 2
Kurtis Nusbaum Avatar answered Oct 23 '22 21:10

Kurtis Nusbaum