Calling derived constructor from base static method - c++

I would like to know if there would be a way to instantiate a derived class from a static method defined on its base class. For now I have wrapped method in a templated struct. Ideally I would like to do something like this:
class Base {
public:
Base(){}
virtual ~Base(){}
static void do_something() {
Base b(); // should call Derived() instead
// use our object
}
}
class Derived: public Base {
public:
Derived(): Base() {}
~Derived(){}
}
// The idea would be to use the derived static method like that
int main(){
Derived::do_something();
}

Related

How to make a derived class function always call the same base class function when called?

How do I make a base class virtual function so that it always is called when a derived class calls its version of the function? I know I can call the base classes function by writing something like BaseClass::foo() at the beginning of the the function DerivedClass::foo(), but what if I want it to be called by default, without the creator of the derived class even knowing that the base classes function does anything?
class BaseClass
{
BaseClass();
virtual void foo() {
printf("base");
}
}
class DerivedClass : public BaseClass
{
DerivedClass();
void foo() {
printf("derived");
}
}
int main()
{
DerivedClass dc;
dc.foo();
}
Should print:
base
derived
That's not directly possible. You could split the function in non-virtual foo on the base class and (pure) virtual fooCore on the derived classes:
class Base {
protected:
virtual void fooCore() = 0;
public:
void foo(){
// do stuff, then call method of derived class
this->fooCore();
}
};
class Derived {
protected:
void fooCore() override {
//actual
};
};
From the "outside" the call Base::foo() stays the same.

Calling a subclass from mainclass

I have a base class Board_S and 10 other classes that inherit the base Board_S class.
I have an object for Board_S which calls the Board_S class and its function which should call one more function from a specific sub class.
I am getting the error : Class does not name a type;
How to do I call the subclass function from this main Class.
Please Help
The only way to do this correctly is to declare a virtual function in your base class and implement it in your derived class.
class A
{
public:
A() {}
virtual ~A() {}
virtual void func() {}
void CallFunc() { func(); }
};
class B : public A
{
public:
B() {}
virtual ~B() {}
virtual void func() {} // overrides it
};
int main()
{
A a;
A* pB = new B;
a.CallFunc(); // will call the base class version of func()
pB->CallFunc(); // will call the derived class version of func()
delete pB;
}
If you don't need the function overridden in a derived class (and the function isn't declared as a pure virtual function), you do not override it.

Abstract interface inheritance

Must virtual methods be always implemented in derived class?
Can I write something like this?
<!-- language: lang-cpp -->
class BaseInterface
{
public:
virtual void fun_a() = 0;
virtual void fun_b() = 0;
virtual ~BaseInterface();
};
class Derived : public BaseInterface
{
void fun_a() { ... };
};
class FinalClass : public Derived
{
void fun_b() { ... };
}
int main()
{
FinalClass test_obj;
test_obj.fun_a(); // use Derived implementation or fail ???
test_obj.fun_b(); // use own implementation
BaseInterface* test_interface = new FinalClass();
test_interface->fun_a(); // fail or ok ???
test_interface->fun_b();
}
Is the code above correct?
Does another virtual method outflank exist?
Pure virtual methods always must be reimplemented in derived class?
Actually a derived class which is going to be instantiated.
In your code, you didn't make an object from Derived so it's OK.
Can i write something like this?
Yes.
You had some minor errors that I corrected them:
class BaseInterface
{
public:
virtual void fun_a() = 0;
virtual void fun_b() = 0;
virtual ~BaseInterface() {}; // You forget this
};
class Derived : public BaseInterface
{
public:
void fun_a() {} // This should be public as its base
};
class FinalClass : public Derived
{
public:
void fun_b() {} // This should be public as its base
};
int main()
{
FinalClass test_obj;
test_obj.fun_a();
test_obj.fun_b();
BaseInterface* test_interface = new FinalClass();
test_interface->fun_a();
test_interface->fun_b();
}
It makes Derived also an abstract class which you cannot instantiate, seeing you don't implement all the virtual functions from it's base, it becomes an abstract class you cannot directly instantiate.
See here: liveworkspace.org/code/6huYU$10
For the rest, your code should work.
Code is correct.
There is no special concept for interface in C++. All are classes. Some of the class methods can be pure virtual. It only means that compiler cannot create an instance of such class.

virtual inheritance query

class Base {
public:
Base(){ }
virtual void Bfun1();
virtual void Bfun2();
};
class Derv : public Base {
public:
Derv(){ }
void Dfun1();
};
Is there a difference between above definitions and the below ones ? Are they same ? if not how both are the different functionally ?
class Base {
public:
Base(){ }
void Bfun1();
void Bfun2();
};
class Derv : public virtual Base {
public:
Derv(){ }
void Dfun1();
};
They are completely different. The first set defines Bfun1 and Bfun2 as virtual function, that allows overriding them in the derived class and call those in the derived class through a base class pointer:
// assume you've overridden the functions in Derived
Base* base_ptr = new Derived;
base_ptr->Bfun1(); // will call function in derived
The second set, however, they're just normal functions. Instead, you declared the base class to be virtual, which has many implications you best read about in a good C++ book or search through the SO questions, I think we have one on that topic.

Non-Virtual Interface - how to invoke the correct virtual function

I have a hierarchy that looks something like this:
class Base
{
public:
void Execute();
virtual void DoSomething() = 0;
private:
virtual void exec_();
};
class Derived : public Base
{
public:
//DoSomething is implementation specific for classes Derived from Base
void DoSomething();
private:
void exec_();
};
void Base::Execute()
{
// do some work
exec_(); //work specific for derived impl
// do some other work
}
void Derived::DoSomething()
{
//impl dependent so it can only be virtual in Base
}
int main()
{
Derived d;
Base& b = d;
b.Execute(); //linker error cause Derived has no Execute() function??
}
So the question is how do I call Execute() using this pattern when I create a derived using my Base class. In my case I do not want to create Derived directly, as I have multiple classes derived from Base and depending on some condition I have to pick a different derived class.
can anyone help?
This
class Base
{
public:
void Execute();
private:
virtual void exec_() {}
};
class Derived : public Base
{
private:
void exec_() {}
};
void Base::Execute()
{
// do some work
exec_(); //work specific for derived impl
// do some other work
}
int main()
{
Derived d;
Base& b = d;
b.Execute();
}
compiles, links, and runs for me.
You should probably also make exec_() pure virtual in your base class. You then also need to implement it in your derived classes.
You need to write a function definition for exec_() function.