Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ RTTI

Posted on 2002-05-14
6
Medium Priority
?
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Accepted Solution

by:
snoegler earned 150 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

719 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