I need to know how this is done in C++ programming
c++ programming: How to find out the child class name inside a method of a parent class if the method of the parent class was called by an object of the child class
I need to know how this is done in C++ programming
c++ programming: How to find out the child class name inside a method of a parent class if the method of the parent class was called by an object of the child class
Did you mean getting a string that stores the derived class' name?
Take a look at How can I get the class name from a C++ object?
You can use the "typeid" approach in tandem with virtual functions to achieve the goal of printing the derived class' name by calling the method from the parent class.
I think he wants to know the exact child class from which the non-virtual parent method is called. For example:
class B
{
public:
void Foo()
{
// Need the child class from which Foo() is called (id or name string)
}
static void SFoo()
{
// Get the child class from which SFoo() is called (id or name string)
}
}
class D1: public B
{
// D1 members
}
class D2: public B
{
// D2 members
}
main()
{
D1 d1;
D2 d2;
d1.Foo();
d2.Foo();
D1::SFoo();
D2::SFoo();
}