Solved

C++ RTTI

Posted on 2002-05-14
6
330 Views
Last Modified: 2010-04-02
Hi i wrote a simple c++ code but when i compiled it using Microsoft Visual C++ 6.0 IDE .i gives me Warning "dynamic_cast used on polymorphic type class D with /GR- option unpredictable behaviour my result".And when i execute it give error "Abnormal Program termination " without giving any output.I am in great trouble help me.The code I am using is follows:---


/******************************************************/
//////////             RTTI            ////////////
/******************************************************/

#include <iostream.h>
class A
{
     public :
     virtual void print(void)const{
          cout<<"I am A"<<endl;
     }


};
class B: public A
{
public :
     virtual void print(void)const{
          cout<<"I am B"<<endl;
     }


};

class C: public A
{
public:
     virtual void print(void)const{
          cout<<"I am C"<<endl;
     }

};


class D
{

 public:
          virtual void print(void) const{
          cout<<"I am D"<<endl ;
     }

};

class E:public B,C,D
{
     public:
          virtual void print(void)const{
          cout<<"I am E"<<endl ;
     }


};

void cmprint(D *d)
{
     E *pe;
     pe=dynamic_cast<E *>(d);
     pe->print();
}
void main()
{

     D *pd=new D;
     cmprint(pd);


}


0
Comment
Question by:Tsaini
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:GGRUNDY
ID: 7007909
In the C/C++ tab of the Project Settings ensure that you you have checked then "Enable Run-Time Type Information RTTI" box of the "C/C++ Language" Category options.
0
 

Author Comment

by:Tsaini
ID: 7008025
Now it Terminates abnormally when i call print print method using pointer "pe" inside "cmprint" function ie (pe->print())
0
 
LVL 6

Expert Comment

by:snoegler
ID: 7008152
Yup, because the cast is not possible and so 'pe' is NULL after the cast. You're trying to cast a 'D' object to a 'E' object, which is not possible if the 'D' object is really a 'D' object. If the 'D' object would be really a pointer to an 'E' object, and then you'd cast the 'D' object (really being an 'E' object) again to an 'E' object, it should succeed.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Accepted Solution

by:
snoegler earned 50 total points
ID: 7008161
IOW, change this line:
    D *pd=new D;

to
    D *pd=dynamic_cast<D*>(new E);

Then it would work, if this cast would be accessible (you'd have to change the access rights of 'E' to make this work).

    D *pd=(D*)(new E);

works with your current code.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 7008516
Thanks. Why did you choose grade 'B'? You can always ask further questions, and i did just post a comment, not an answer ...
0
 

Author Comment

by:Tsaini
ID: 7008750
As i have given u the entire code but even then ur answer is not  appropreate as per my question ,But it was very good starting point for me As in ur comment u r writing


************************************************
 "IOW, change this line: D *pd=new D;

to  D *pd=dynamic_cast<D*>(new E);

Then it would work, if this cast would be accessible (you'd have to change the access rights of 'E'
to make this work).

   D *pd=(D*)(new E);

works with your current code. "
****************************************************


THE ENTIRE CODE WAS IN FRONT OF U, EVEN THEN U COULD NOT  GIVE THE ACCURATE SOLUTION .U ARE WRITING AS SHOWN ABOBE "if this cast would be accessible (you'd have to change the access rights of 'E'to make this work)"
AS IN MY QUESTION THE CAST IS NO WAY ACCESSIBLE.
MEANS U DIDNT GO THOUGH THE CODE THOROUGHLY.IN ORDER TO MAKE THE CODE WORKING I HAVE MADE SOME MORE CORRECTIONS.THATS WHY I HAVE GIVEN THE GRADE GOOD.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now