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);
}
Derived* temp = dynamic_castDerived*(a) - Why doesn't it work?
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 :
I'd greatly appreciate any suggestions! Thanx
Perhaps you forgot to turn on RTTI (Run-Time Type Information). In VS, it is off by default.
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 JohnBoltonThe 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..?
Perhaps you forgot to turn on RTTI (Run-Time Type Information). In VS, it is off by default.
Quote:Original post by ExtrariusQuote:Original post by JohnBoltonThe 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..?
Perhaps you forgot to turn on RTTI (Run-Time Type Information). In VS, it is off by default.
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
Quote:Original post by Conner McCloudIn 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
[...]Quote:[...]
"warning C4541: 'dynamic_cast' used on polymorphic type 'Base' with /GR-; unpredictable behavior may result"
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
Popular Topics
Advertisement