Solved

C++ RTTI

Posted on 2002-05-14
6
334 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

803 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