Re: Accessing virtuals in base class
Jo wrote:
Jo wrote:
OK, what i want to do is having a kind of run time type information in
a basic way:
[CUT]
So this way the exact actual object type can be recalled, but also
wether it's actually derived from another type!
Or am i on a totally wrong path here?
Maybe i should use RTTI? But isn't that taking a lot of speed/memory
resources?
It's about a realtime code working on interupt level, so it must be
steady and fast code! (e.g. no mem allocs)
It's a interesting problem, it makes sense (at compile time you should
be able to construct such a function that returns true for a set of
values, based on the inheritance of each class) and it shouldn't require
memory allocation. I think the solution is based on template, I've tried
to sketch a solution that uses an intermediate template class that has
as arguments the Actual class (CRTP) and the parent.
#include <string>
#include <iostream>
class Figure
{
public:
virtual ~Figure() {}
virtual std::string GetName() const = 0;
virtual bool IsType(const std::string name) const { return name ==
"Figure"; }
};
template <class Parent, class Actual>
class FigureT
: public Parent
{
public:
virtual std::string GetName() const { return "Figure"; }
virtual bool IsType(const std::string name) const { return name ==
static_cast<const Actual*>(this)->Actual::GetName() ||
Parent::IsType(name); }
};
class Square
: public FigureT<Figure,Square>
{
public:
virtual std::string GetName() const { return "Square"; }
};
class Ellipse
: public FigureT<Figure,Ellipse>
{
public:
virtual std::string GetName() const { return "Ellipse"; }
};
class Circle
: public FigureT<Ellipse,Circle>
{
public:
virtual std::string GetName() const { return "Circle"; }
};
int main()
{
Square sq;
std::string type;
char* yes = "yes";
char* no = "no";
type = "Square";
std::cout << "is sq a " << type << "? " << (sq.IsType(type) ? yes : no)
<< "\n";
type = "Figure";
std::cout << "is sq a " << type << "? " << (sq.IsType(type) ? yes : no)
<< "\n";
type = "Ellipse";
std::cout << "is sq a " << type << "? " << (sq.IsType(type) ? yes : no)
<< "\n";
Ellipse el;
type = "Ellipse";
std::cout << "is el a " << type << "? " << (el.IsType(type) ? yes : no)
<< "\n";
type = "Figure";
std::cout << "is el a " << type << "? " << (el.IsType(type) ? yes : no)
<< "\n";
type = "Circle";
std::cout << "is el a " << type << "? " << (el.IsType(type) ? yes : no)
<< "\n";
Circle ci;
type = "Figure";
std::cout << "is ci a " << type << "? " << (ci.IsType(type) ? yes : no)
<< "\n";
type = "Ellipse";
std::cout << "is ci a " << type << "? " << (ci.IsType(type) ? yes : no)
<< "\n";
type = "Circle";
std::cout << "is ci a " << type << "? " << (ci.IsType(type) ? yes : no)
<< "\n";
}
I think you can start from here and improve this solution that is
somewhat messy. The concept is to implement some sort of recursion on
the function calls based on the templates arguments. With multiple
inheritance it won't work due to ambiguities... maybe it can be improved.
Regards,
Zeppe