Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

C++ RTTI

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
Tsaini
Asked:
Tsaini
  • 3
  • 2
1 Solution
 
GGRUNDYCommented:
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
 
TsainiAuthor Commented:
Now it Terminates abnormally when i call print print method using pointer "pe" inside "cmprint" function ie (pe->print())
0
 
snoeglerCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
snoeglerCommented:
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
 
snoeglerCommented:
Thanks. Why did you choose grade 'B'? You can always ask further questions, and i did just post a comment, not an answer ...
0
 
TsainiAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now