Hi, I am getting errors with RTTI. In this program I have an Abstract base class with virtual function area() and two derived classes.

I defined a polymorphic function and passed the derived object as argument to it which has reference to the base class as argument. I used typeid opeator to find RTTI and execute a function which is not defined in the Abstract base class. But I am getting the error as follows.

class poly
{
public:
virtual void area(void)=0;
};
class square : public poly
{
void area(void ) { cout<<" area : square " ;}
virtual void display(void) { cout<<" I am square ";}
};
class rect : public poly
{
void area(void) { cout<<" area : rectangle ";}
};
void print(poly &abc)
{
abc.area();
if(typeid(abc)==typeid(square))
abc.display();
}
int main()
{
square s;rect r;
print(s);
}

Eroor : class poly doesnot have disp function as member

could you please help in finding out the reason behind this error

Nick Evan commented: code tags on first post award! +27

Recommended Answers

All 2 Replies

Eroor : class poly doesnot have disp function as member

could you please help in finding out the reason behind this error

The compiler is right, "class poly" doesn't have a disp() function as a member. But the class 'square' does.

So change this line: void print(poly &abc) to this: void print(square &abc) and that should solve your problem.

Also don't forget to make the other memberfunction public: !

Use static_cast,

void print(poly &abc){
   abc.area();
   if(typeid(abc)==typeid(square))
      static_cast<square*>(&abc)->display();
}

PS: display() must have public access.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.