I want to create a class constructor which will call it's superclass constructor only on certain condition. My current implementation of this is shown below.
class MyClass : public OtherClass
{
public:
template<typename... Args>
MyClass(bool condition, Args&&... args)
{
if(condition)
{
*(OtherClass*)this = OtherClass(args...);
}
else
{
// Unrelated stuff
}
}
};
I can't use MyClass(...) : OtherClass(...) {} syntax here because superclass's constructor should not be called every time.
Is there any way to call superclass constructor directly instead of calling move constructor (as shown in my example).
You could create 2 different constructors for both the base- and the derived class.
The constructor of the derived class calls the appropriate constructor of the base class.
A static method within the derived class creates an instance based on the passed arguments.
Something like this:
class OtherClass
{
public:
OtherClass()
{
...
}
OtherClass(Args&... args)
{
...
}
} // class OtherClass
class MyClass: public OtherClass
{
private:
MyClass(): OtherClass()
{
...
}
MyClass(Args&... args): OtherClass(args)
{
...
}
public:
static MyClass* createInstance(bool condition,
Args&&... args)
{
if (condition)
return (new MyClass());
else
return (new MyClass(args));
}
} // class MyClass
A solution is to not do this in the constructor, but in a helper function. For example:
class BaseClass {
BaseClass() {}
BaseClass(int x) {setX(x);}
void setX(int x) {
//thing
}
}
class Derived : BaseClass {
Derived(int x) {
if (x>10) setX(x);
}
}
You could overload the constructor in the following way to implement tag dispatching:
struct PassArgs {};
struct DoNotPassArgs {};
class MyClass: public OtherClass {
public:
template<typename... Args>
MyClass(PassArgs, Args&&... args) : OtherClass(args...) {}
template<typename... Args>
MyClass(DoNotPassArgs, Args&&...) : OtherClass() {}
};
Above answer from Robert Kock is not perfectly correct. He is calling Base class (supper class) constructor from initialization list of derived class constructor but without a condition. But as per your question can we call a base class constructor based on a CONDITION from the BODY of derived class constructor?. The answer is NO we can not. As per object oriented principle A Base class constructor must call and initialized first. In the above answer from Robert Kock, base class constructor is called from the initialization list of drive class constructor before control enter inside the body and without any condition. In the initialization list you can not even put any condition. So it is not possible to call a based class constructor to call from the body of a derive class constructor. That is the reason for this kind of requirement we introduce one more method called init() in a class. Now the below example can full fill your requirement. Here in this example you have to add one default constructor in your base class, so that this default constructor call first but in that default constructor you are not going to do anything.
Now see the below example:-
#include<iostream>
class B
{
private:
int x;
int y;
public:
B(){std::cout<<"I am B's default constructor"<<std::endl;}//Default constructor not doing anything
void init(int x)
{
std::cout<<"Based init with param x"<<std::endl;
this->x = x; //this method initializing only one member
y = 0;// just initialized with default value 0
}
void init(int x,int y)
{
std::cout<<"Based init with param x and y"<<std::endl;
this->x = x; // here we initializing all the members
this->y = y;
}
void print()
{
std::cout<<"x ="<<x<<std::endl;
std::cout<<"y ="<<y<<std::endl;
}
};
class D : public B
{
public:
D(int i)
{
std::cout<<"I am D's constructor"<<std::endl;
if( i == 1 )
B::init(3);
else
B::init(4, 5);
}
};
int main()
{
std::cout<<"First Example"<<std::endl;
D *d = new D(1);
d->print();
std::cout<<"Now second Example"<<std::endl;
D *d1 = new D(2);
d1->print();
return 0;
}
Related
So shortly the situation is like this
class Base
{
public:
Base() { setZero();}
virtual void setZero() {std::cout << "Set all Base class values to zeros (default) values";}
};
class Derived : public Base
{
public:
Derived () { }
void setZero() override {
Base::setZero();
std::cout << "Set all Derived class values to zeros (default) values";
}
};
setZero is public an is called form different places, also it has some logic, not just assignments, as Base and Derived classes are quite large.
But it's all doesn't work as intended as dynamic binding doesn't work when function is called from the constructor.
I see the solution to duplicate code from setZero to the consructors, but duplication of code is a bad thing. Is there some other solutions?
You might have factory to have "post-call", something like:
template <typename T, typename ... Ts>
T CreateBaseType(Ts&&... args)
{
T t(std::forward<Ts>(args)...);
t.setZero();
return t;
}
TL;DR - two phase construction sucks. Try to make your constructors construct stuff, and not call any virtual methods, or require it in order to function.
If you want initialization to occur after object construction (including vtables), you need to have a separate initialization phase on your objects.
A probably better way to handle this is this:
class Base
{
int x = 0; // notice the =0 here
public:
Base() {} // nothing
virtual setZero() {*this = Base{};} // use operator= to assign zeros
};
class Derived : public Base
{
double d = 0.; // notice the = 0. here
public:
Derived () { } // nothing
void setZero() override {*this = Derived{};}
};
we can avoid rewriting setZero as well:
template<class D, class B=void>
struct SetZero:B {
void setZero() override {
*static_cast<D*>(this) = D{};
}
};
template<class D>
struct SetZero<D,void> {
virtual void setZero() {
*static_cast<D*>(this) = D{};
}
};
now we can:
class Base:public SetZero<Base>
{
int x = 0; // notice the =0 here
public:
A() {} // nothing
};
class Derived : public SetZero<Derived, Base>
{
double d = 0.; // notice the = 0. here
public:
Derived () { } // nothing
};
and setZero is written for us.
The DRY here is that default construction zeros, and we put the zeros right next to where we declare variables. setZero then just becomes a helper method to copy over yourself with a default constructed object.
Now, exposing value semantics copy/move operations on a class with a vtable is a bad plan. So you probably want to make the copy/move protected and add friend declarations.
template<class D, class B=void>
struct SetZero:B {
void setZero() override {
*static_cast<D*>(this) = D{};
}
SetZero()=default;
protected:
SetZero(SetZero&&)=default;
SetZero& operator=(SetZero&&)=default;
SetZero(SetZero const&)=default;
SetZero& operator=(SetZero const&)=default;
~SetZero() override=default;
};
template<class D>
struct SetZero<D,void> {
virtual void setZero() {
*static_cast<D*>(this) = D{};
}
SetZero()=default;
protected:
SetZero(SetZero&&)=default;
SetZero& operator=(SetZero&&)=default;
SetZero(SetZero const&)=default;
SetZero& operator=(SetZero const&)=default;
virtual ~SetZero()=default;
};
so those get longer.
In Base and Derived as they have vtables, you are recommended to add
protected:
Derived(Derived&&)=default;
Derived& operator=(Derived&&)=default;
};
to block external access to move/copy construct and move/copy assign. This is advised regardless of how you write setZero (any such move/copy is going to risk slicing, so exposing it to all users of your class is a bad plan. Here I make it protected, because setZero relies on it to make zeroing DRY.)
Another approach is a two-phase construction. In it, we mark all "raw" constructors are protected.
class Base {
int x;
protected:
Base() {} // nothing
public:
virtual setZero() { x = 0; }
};
we then add a non-constructor constructor:
class Base {
int x;
protected:
Base() {} // nothing
public:
template<class...Ts>
static Base Construct(Ts&&...ts){
Base b{std::forward<Ts>(ts)...};
b.setZero();
}
virtual setZero() { x = 0; }
};
and external users have to Base::Construct to get a Base object. This sort of sucks, because our type is no longer regular, but we already have vtable, which makes it unlikely to be regular in the first place.
We can CRTP it;
template<class D, class B=void>
struct TwoPhaseConstruct:B {
template<class...Ts>
D Construct(Ts&&...ts) {
D d{std::forward<Ts>(ts...));
d.setZero();
return d;
}
};
template<class D>
struct TwoPhaseConstruct<D,void> {
template<class...Ts>
D Construct(Ts&&...ts) {
D d{std::forward<Ts>(ts...));
d.setZero();
return d;
}
};
class Base:public TwoPhaseConstruct<Base> {
int x;
protected:
Base() {} // nothing
public:
virtual setZero() { x = 0; }
};
class Derived:public TwoPhaseConstruct<Derived, Base> {
int y;
protected:
Derived() {} // nothing
public:
virtual setZero() { Base::setZero(); y = 0; }
};
and here goes down the rabbit hole, if you want to make_shared or similar we have to add a helper type.
template<class F>
struct constructor_t {
F f;
template<std::constructible_from<std::invoke_result_t<F const&>> T>
operator T()const&{ f(); }
template<std::constructible_from<std::invoke_result_t<F&&>> T>
operator T()&&{ std::move(f)(); }
};
which lets us
auto pBase = std::make_shared<Base>( constructor_t{[]{ return Base::Construct(); }} );
but how far down the rabbit hole do you want to go?
Alternatively to the other answers, separating functionality from API lets you use the general flow you want while dodging the whole "using the vtable in the constructor" issue.
class Base
{
public:
Base() {
setZeroImpl_();
}
virtual void setZero() {
setZeroImpl_();
}
private:
void setZeroImpl_() {
std::cout << "Set all Base class values to zeros (default) values";
}
};
class Derived : public Base
{
public:
Derived () {
setZeroImpl_();
}
void setZero() override {
Base::setZero();
setZeroImpl_();
}
private:
void setZeroImpl_() {
std::cout << "Set all Derived class values to zeros (default) values";
}
};
You could solve it this way:
#include <iostream>
class Base
{
public:
Base() { Base::setZero();}
virtual void setZero() {std::cout << "Set all Base class values to zeros (default) values\n";}
protected:
Base(bool) {};
};
class Derived : public Base
{
public:
Derived () : Base(true) { Derived::setZero(); }
void setZero() override {
Base::setZero();
std::cout << "Set all Derived class values to zeros (default) values\n";
}
};
What I have done, is the following:
Make clear which setZero() method is called by which constructor
Added call to setZero()also from the Derived constructor
Added a protected Base constructor that does not call its setZero() method, and calling this constructor from Derived's constructor, so that Base::setZero()is called exactly once during creation of a Derived object.
By doing it this way, you can create Base or Derived and call zerZero() as intended.
You could implement a simple factory method in your Derived class and remove the setZero() calls from the constructors alltogether. Then making the constructors non-public will tell consumers of the class to use the factory method for proper instantiation instead of the constructor. Something like this:
class Base
{
protected:
Base() { }
virtual void setZero() {std::cout << "Set all Base class values to zeros (default) values";}
};
class Derived : public Base
{
public:
static Derived createInstance()
{
Derived derived;
derived.setZero();
return derived;
}
private:
Derived() { }
void setZero() override {
Base::setZero();
std::cout << "Set all Derived class values to zeros (default) values";
}
};
And then create your instance of Derived somehow like this:
int main()
{
Derived derived = Derived::createInstance();
// do something...
return 0;
}
With this approach you can also make sure that no one can create an instance of your class that is not in a valid state.
Note: Don't know if you use the base class at some places directly but if this is the case you could provide a factory method for it as well.
If I understand your question correctly, then what you need to do is below simply
#include <iostream>
using std::cout;
using std::endl;
class Base
{
void init() {std::cout << "Set all Base class values to zeros (default) values" << endl;}
public:
Base() {init(); }
virtual void setZero() {init();}
};
class Derived : public Base
{
void init() { std::cout << "Set all Derived class values to zeros (default) values" << endl; }
public:
Derived () { init(); }
void setZero() override {
Base::setZero();
init();
}
};
int main()
{
Derived d1;
cout << endl;
d1.setZero();
}
You wrote the below statement for your code
But it's all doesn't work as intended as dynamic binding doesn't work when function is called from the constructor.
Yes, the virtual behavior will not work, when calling setZero() from the base class constructor, and the reason is that derived class has not been constructed yet.
What you need is to initialize each class when its constructed, and that should happen in there respective constructors, and that is what we do in the above code.
Base class constructor will call its own setZero, derived class constructor will call its own setZero.
And you will continue to do the same thing, if you derive any further class from Derived class.
Why does this code:
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
};
int main(void)
{
B *b = new B(5);
delete b;
}
Result in these errors:
main.cpp: In function ‘int main()’:
main.cpp:13: error: no matching function for call to ‘B::B(int)’
main.cpp:8: note: candidates are: B::B()
main.cpp:8: note: B::B(const B&)
Shouldn't B inherit A's constructor?
(this is using gcc)
If your compiler supports C++11 standard, there is a constructor inheritance using using (pun intended). For more see Wikipedia C++11 article. You write:
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
using A::A;
};
This is all or nothing - you cannot inherit only some constructors, if you write this, you inherit all of them. To inherit only selected ones you need to write the individual constructors manually and call the base constructor as needed from them.
Historically constructors could not be inherited in the C++03 standard. You needed to inherit them manually one by one by calling base implementation on your own.
For templated base classes, refer to this example:
using std::vector;
template<class T>
class my_vector : public vector<T> {
public:
using vector<T>::vector; ///Takes all vector's constructors
/* */
};
Constructors are not inherited. They are called implicitly or explicitly by the child constructor.
The compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). But if you want a constructor that will accept an int, you have to define it explicitly.
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
public:
explicit B(int x) : A(x) { }
};
UPDATE: In C++11, constructors can be inherited. See Suma's answer for details.
This is straight from Bjarne Stroustrup's page:
If you so choose, you can still shoot yourself in the foot by inheriting constructors in a derived class in which you define new member variables needing initialization:
struct B1 {
B1(int) { }
};
struct D1 : B1 {
using B1::B1; // implicitly declares D1(int)
int x;
};
void test()
{
D1 d(6); // Oops: d.x is not initialized
D1 e; // error: D1 has no default constructor
}
note that using another great C++11 feature (member initialization):
int x = 77;
instead of
int x;
would solve the issue
You have to explicitly define the constructor in B and explicitly call the constructor for the parent.
B(int x) : A(x) { }
or
B() : A(5) { }
How about using a template function to bind all constructors?
template <class... T> Derived(T... t) : Base(t...) {}
Correct Code is
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
public:
B(int a):A(a){
}
};
main()
{
B *b = new B(5);
delete b;
}
Error is b/c Class B has not parameter constructor and second it should have base class initializer to call the constructor of Base Class parameter constructor
Here is how I make the derived classes "inherit" all the parent's constructors. I find this is the most straightforward way, since it simply passes all the arguments to the constructor of the parent class.
class Derived : public Parent {
public:
template <typename... Args>
Derived(Args&&... args) : Parent(std::forward<Args>(args)...)
{
}
};
Or if you would like to have a nice macro:
#define PARENT_CONSTRUCTOR(DERIVED, PARENT) \
template<typename... Args> \
DERIVED(Args&&... args) : PARENT(std::forward<Args>(args)...)
class Derived : public Parent
{
public:
PARENT_CONSTRUCTOR(Derived, Parent)
{
}
};
derived class inherits all the members(fields and methods) of the base class, but derived class cannot inherit the constructor of the base class because the constructors are not the members of the class. Instead of inheriting the constructors by the derived class, it only allowed to invoke the constructor of the base class
class A
{
public:
explicit A(int x) {}
};
class B: public A
{
B(int x):A(x);
};
int main(void)
{
B *b = new B(5);
delete b;
}
I know it's OK to call base class function in a derived class constructor, because base class is constructed before derived class.But I'm not sure if this is a good practice.Example code is:
class Base {
public:
int Get() const { return i_; }
void Set(const int i) { i_ = i; }
private:
int i_{0};
};
class Derived : public Base {
// initialize `derived_i_` with a call to base class function, Is this a good
// practice in production code?
Derived() : derived_i_{Get()} {
// do some other things
}
private:
int derived_i_{0};
};
To be more pedantic, you could write your constructor as the following:
Derived() : Base(), derived_i_{Get()} {
// do some other things
}
The compiler should fully construct the base class before doing any initialization of the derived class.
I've two classes called FWindow and FFramwWindow. The FFramwWindow class inherits the FWindow. The FWindow class contains two constructor method.
The first one is default constructor and the second one contains one parameter of int type.
I call the second constructor from the FFramwWindow class default constructor to initialize a member variable of the FWindow class called 'value'.
But I don't know why it isn't working -
class FWindow {
public:
int value;
FWindow()
{
this->value = 0;
}
FWindow(int val)
{
this->value = val;
}
};
class FFramwWindow : public FWindow
{
public:
FFramwWindow()
{
FWindow::FWindow(6);
printf("value %d\n", this->value);
}
};
int main(int argc, _TCHAR* argv[])
{
FFramwWindow obj;
return 0;
}
The above code prints - value 0
Where I expected it will print - value 6
Seems it's only calling the default base class constructor, not the second one that I called explicitly.
Note: I'm using Visual Studio 2008
Because you should do the following in constructor:
FFramwWindow() : FWindow(6)
{
....
In your original code you create a local (in constructor scope) object of FWindow.
the code
FWindow::FWindow(6);
is not a call to the parent constructor, but the creation of a local instance of FWindow. The correct syntax in C++ to specify which FWindow constructor should be called is
FFramwWindow() : FWindow(6)
{
...
}
If you do not specify the constructor to use for the base class (and for data members), C++ uses the default constructor, that is
FFramwWindow()
{
...
}
is equivalent to
FFramwWindow() : FWindow()
{
...
}
Note that if you have multiple inheritance you should constructors for each base class separating them with comma. As bonus information, the base constructors are called in the order specified in the inheritance definition, not those in which you specify them in the constructor:
class A {
A();
A(int n);
A(string s);
};
class B {
B(int n = 6);
}
class C {
C();
C(float x);
}
class D: public A, public B, public C {
D();
}
D::D() : C(3),A(5)
{
}
In this example, creating an instance of D will invoke in order the constructors for A(5), B(6), C(3.0), D()
You must call the constructor function of the base class when you declare the derived class's constructor. Consider this example:
#include<iostream>
class base
{
public:
int i;
base()
{
i = 0;
}
base(int p)
{
i = p;
}
};
class derived1: public base
{
public:
derived1():base()
{
std::cout<<i<<std::endl; //i is 0 here
}
};
class derived2: public base
{
public:
derived2():base(10)
{
std::cout<<i<<std::endl; //i is 10 here
}
};
class derived3: public base
{
public:
derived3(int x):base(x)
{
std::cout<<i<<std::endl;
//this takes an argument from the derived class's constructor
//as base class's constructor's parameter
}
};
int main()
{
derived1 d1;
derived2 d2;
derived3 d3(9);
return 0;
}
In derived1 class, the first constructor of the base class is called. In derived2 class, the second constructor is called. And in third case, the constructor function takes an argument which is passed to the base class's constructor (That means we are using the second constructor of base class).
In your code,You didn't call the base class's constructor which by default calls the constructor that takes no argument. That's why it is outputting 0.
FWindow::FWindow(6);
This statement just creates a temporary new object of class FWindow which is destroyed right after this statement is executed. Constructor functions are meant to be called automatically when you create a object. They are not meant to be called manually.
You can find some explanation here: http://www.geeksforgeeks.org/possible-call-constructor-destructor-explicitly/
I have the following situation:
Factory class implementation:
class FactoryClassA {
public:
FactoryClassA(){};
~FactoryClassA(){};
ClassA create(double a, double b) {
return ClassA;
};
};
ClassA implementation:
class ClassA {
friend class FactoryClassA
~FactoryClassA() {}
private:
ClassA(double a = 0, double b = 0)
{
a(a),
b(b)
};
double a;
double b;
};
OtherClass implementation:
class OtherClass {
public:
OtherClass() {
ClassFactory myClassAFactory;
aClassA = myClassAFactory.create();
}
~OtherClass() {};
private:
aClassA;
};
Unfortunately, this does not work. Because in the (empty) member initializer list of OtherClass, the empty constructor of ClassA is called, which does not exist.
The reason why I made the constructor of ClassA private, is that I want the user to only create objects of ClassA through the FactoryClassA.
First of all, create() should have a return type:
ClassA create(double a, double b) {
return {a, b};
}
I am not going through all the other syntactic bugs. Please fix them, because others might find your question when they search for answers. Please make it easy for them to understand your question.
This has to be in your code after ClassA has been defined, because the compiler has to know the size of a ClassA object as well as the constructor to use.
Second, just initialize your members before the constructor body:
OtherClass() :
aClassA{ClassFactory{}.create(0.0, 0.0)}
{}
You might prefer to have create() as a static member function of ClassFactory. Then the OtherClass constructor looks like this:
OtherClass() :
aClassA{ClassFactory::create(0.0, 0.0)}
{}
That is more intuitive.
This code is full of small errors, that make it unuseable. First syntax for member initialization for a class is :
class A {
A(x, y): m_a(x) { ... }
...
}
and class declaration are statements so they need the final ;
Next, the dtor for a class must be public if you want to be able to use temporary objects.
The default values should not be in ClassA ctor but on create factory method.
Finally, you should use a static factory in OtherClass, no need to create a new factory for each object.
Code becomes:
class FactoryClassA;
class ClassA {
public:
friend class FactoryClassA;
~ClassA(){}
private:
ClassA(double a, double b):a(a),b(b){}
double a;
double b;
};
class FactoryClassA {
public:
FactoryClassA(){}
~FactoryClassA(){}
ClassA create(double a = 0, double b = 0) {
return ClassA(a, b);
}
};
class OtherClass {
public:
OtherClass(): aClassA(myClassAFactory.create()){}
~OtherClass(){}
private:
static FactoryClassA myClassAFactory;
ClassA aClassA;
};
FactoryClassA OtherClass::myClassAFactory;
Not far from original one, but this ones compiles...