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

Started by
6 comments, last by Conner McCloud 18 years, 10 months ago
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
Advertisement
Perhaps you forgot to turn on RTTI (Run-Time Type Information). In VS, it is off by default.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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
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..?
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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
Ah I see, excellent, thanks guys!
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

"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Extrarius
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

I've got 2003.

CM

This topic is closed to new replies.

Advertisement