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.
Related
class Base
{
public:
virtual void f()
{
g();
}
virtual void g()
{
cout<<"base";
}
};
class Derived : public Base
{
public:
virtual void f()
{
Base::f();
}
virtual void g()
{
cout<<"derived";
}
};
int main()
{
Base *pBase = new Derived;
pBase->f();
return 0;
}
In this program I have kept both derived and base class functions as virtual. Is it possible call virtual functions of derived class through base class pointer and base class functions are not virtual.
Thanks in advance..
assuming functions in base class are not virtual
This can be achieved via type erasure. But there are caveats.
Your "base" class should decide between the two:
Being a view class (can't be called delete on or created by itself)
Being a resource owning class (implemented similar to 1, but stores a smart pointer).
Here is an example for case 1: https://godbolt.org/z/v5rTv3ac7
template <typename>
struct tag{};
class base
{
public:
base() = delete;
template <typename Derived>
explicit base(tag<Derived> t)
: _vTable(make_v_table(t))
{}
int foo() const { return _vTable.foo(*this); }
protected:
~base() = default;
private:
struct v_table
{
virtual int foo(const base &b) const = 0;
protected:
~v_table() = default;
};
template <typename Derived>
static const v_table& make_v_table(tag<Derived>){
struct : v_table
{
int foo(const base &b) const {
return static_cast<const Derived&>(b).foo();
}
} static const _vTable{};
return _vTable;
}
private:
const v_table& _vTable;
};
class derived : public base
{
public:
explicit derived()
: base(tag<derived>{})
{}
int foo() const { return 815; }
};
// example
#include <iostream>
int main(){
derived d{};
const base& b = d;
std::cout << b.foo() << '\n';
}
Take notice, that you can only take a pointer or a reference (cv-qualified) to a base class. The base class can't be created on its own.
Also tag<T> is needed to call a templated constructor.
DO NOT CALL DERIVED METHODS IN THE BASE CONSTRUCTOR OR DESTRUCTOR
Simple answer is no, if the function you are calling is not virtual. The Compiler would have no Idea that you are trying to call a function from the Derived Class, and won't make and I'm paraphrasing here since I do not know the proper term for,"Won't make proper entries in the Virtual Table".
class Base
{
public:
void f()
{
std::cout<<"Base f() Called\n";
g();
}
virtual void g()
{
std::cout<<"Base g()\n";
}
virtual ~Base(){std::cout<<"Base Destroyed\n";}
};
class Derived : public Base
{
public:
void f()
{
g();
}
virtual void g()
{
std::cout<<"Derived g()\n";
}
~Derived(){std::cout<<"Derived Destroyed\n";}
};
int main()
{
Derived* D1 = new Derived();
Base* B1 = D1;
B1->f();
delete B1;
return 0;
}
Have a look at the following code, I have not declared Base::f() as virtual,calling B1->f() calls the Base Method, but the base method calls a virtual function Base::g() and this allows the "Derived" method be called.
Have a look at this thread or this blogpost to understand Virtual Tables.
(1) and you must ALWAYS declare the destructor of a base class virtual when destroying Derived Object through a Base Pointer, else the resources used by the Derived Object will never get destroyed and it's memory will leak until the program closes.
Don't Take my word as gospel, I am simply passing on knowledge I have acquired from first hand experience, Except for (1), specially if you are not using smart pointers
There is another way to call derived class method from a base pointer object without casting?
class Base
{
};
class Derived : public Base
{
public:
void set() {printf("bbbbbbbbbbbb"); }
};
int main()
{
Base* pbBase;
pbBase = new Derived();
//pbBase->set(); // Compilation error.
(Derived*)pbBase->set(); // Working.
}
You can add virtual methods in Base that you override in Derived.
Note that you should make the destructor virtual too or else the derived class' destructor would not get called when you delete the object through a base class pointer.
#include <iostream>
class Base
{
public:
virtual ~Base() = default;
virtual void set() = 0; // = 0 makes it pure virtual
};
class Derived : public Base
{
public:
void set() override { std::cout << "bbbbbbbbbbbb\n"; }
};
int main()
{
Base* pbBase;
pbBase = new Derived();
pbBase->set();
delete pbBase;
}
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.
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();
}
I have the following classes:
class Base {
public:
virtual ~Base(){}
Base() {}
virtual void foo() = 0;
};
class Derived : public Base {
public:
virtual ~Derived(){}
Derived() : Base() {}
void foo() { printf("derived : foo\n"); }
};
class IInterface {
public:
virtual ~IInterface() {}
virtual void bar() = 0;
};
class C : public Derived, public IInterface {
public:
virtual ~C(){}
C() : Derived(){}
void bar() { printf("C : bar\n"); }
};
now I have a bunch of Derived* objects and I want to apply different interfaces on them :
Derived* d = new Derived();
C* c = dynamic_cast<C*>(d);
c->bar();
c->foo();
dynamic_cast returns nullptr and with c-style cast i get seg fault.
is there anyway to achieve this?
note that my objects are already created with Derived ctor.
i just want to treat them differently using Interfaces
The only way to achive this is to create a new object and move the data over from the old object.
Try encapsulating the behaviour that needs to change at runtime. Instead of inheriting from the IInterface, you have a member variable that is an IInterface pointer. Then instead of overriding bar in the child class, you pass the call to bar through to whatever is being pointed at. This allows modular behavior that looks just like polymorphism, but is more flexible:
class IInterface {
public:
virtual ~IInterface() {}
virtual void bar() = 0;
};
class Derived : public Base {
public:
IInterface* m_bar;
virtual ~Derived(){}
Derived() : Base(){}
void bar() {return m_bar->bar(); }
void foo() { printf("derived : foo\n"); }
};
You then derive and create IInterface objects and can associate any of them with Derived objects.