Sign in to follow this  
moagstar

Derived* temp = dynamic_castDerived*(a) - Why doesn't it work?

Recommended Posts

I am passing a pointer to a function, and in the function I want to find out which (out of a number) of derived types the pointer points to, I have the following code (which I think should work) But I get an access violation exception whenever it runs :

class Base
{
public:
  virtual ~Base() {}
};

class Derived : public Base
{
public:
  virtual ~Derived() {}
};

class Derived2 : public Base
{
public:
  virtual ~Derived2() {}
};

void someFunc(Base* p)
{
  if(Derived* temp = dynamic_cast<Derived*>(p))
  {
    // do something
  }
  else if(Derived2* temp = dynamic_cast<Derived2*>(p))
  {
    // do something else
  }
}

void anotherFunc()
{
  Base* obj = new Derived;
  someFunc(obj);
}




I'd greatly appreciate any suggestions! Thanx

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Perhaps you forgot to turn on RTTI (Run-Time Type Information). In VS, it is off by default.

The required compiler switch is /GR, and in .NET 2003 is under "C/C++" "Language".

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Perhaps you forgot to turn on RTTI (Run-Time Type Information). In VS, it is off by default.
The odd thing is that about 25% of the stuff that requires RTTI works fine with it turned on in VS 2002 at least. I wonder how hard it would be for VS to emit an error (or warning at least) if using stuff that requires RTTI when that feature is off..?

Share this post


Link to post
Share on other sites
Quote:
Original post by Extrarius
Quote:
Original post by JohnBolton
Perhaps you forgot to turn on RTTI (Run-Time Type Information). In VS, it is off by default.
The odd thing is that about 25% of the stuff that requires RTTI works fine with it turned on in VS 2002 at least. I wonder how hard it would be for VS to emit an error (or warning at least) if using stuff that requires RTTI when that feature is off..?

Using dynamic_cast results in this:
Quote:

"warning C4541: 'dynamic_cast' used on polymorphic type 'Base' with /GR-; unpredictable behavior may result"

So for it at least, you can add a #pragma warning(error: 4541) to cause that to error. The help page seems like its a generic warning for any RTTI stuff, so I think that'll fix most of it.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
[...]
Quote:

"warning C4541: 'dynamic_cast' used on polymorphic type 'Base' with /GR-; unpredictable behavior may result"
[...]
In what version? I don't think I've ever had that warning before, and I have had problems using RTTI without enabling it before in VS.Net 2002

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this