Solved

C++ RTTI

Posted on 2002-05-14
6
335 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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