Jump to content
  • Advertisement
Sign in to follow this  
moagstar

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

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!