M
mijobee
Hello Everyone,
I just wanted to check that I'm using dynamic_cast correctly. I have a
hierarchy of objects using pure virtual classes and virtual inheritance
to implement interfaces. I ran into a problem using C-style casting to
an instance of a derived class from its interface type to a variable of
its concrete type. I was confused because I thought C-style casts
always compiled even if they wouldn't run correctly at runtime. I
modified the code to use dynamic_cast which seems correct, compiles and
runs but have found some contradictory information about its use so I
wanted to confirm I'm using it correctly. The code below illustrates
my problem. Thanks for your help.
#include <stdio.h>
class IObject
{
public:
virtual bool Equals(IObject *object) = 0;
};
class IList : public virtual IObject
{
public:
virtual IObject *Get(int index) = 0;
};
class Object : public virtual IObject
{
public:
virtual bool Equals(IObject *object) {
return this == object;
}
};
class ArrayList : public Object, public virtual IList
{
public:
virtual IObject *Get(int index) {
return this; // Just return something
}
};
class String : public Object { };
int main(int argc, char *argv[])
{
IList *list = new ArrayList();
IObject *obj = new String();
String *str = (String *)obj;
// This works: String *str = dynamic_cast<String *>(obj);
printf("str %s null.\n\0", str == 0 ? "is" : "is not");
}
Example.cpp: In function 'int main(int, char**)':
Example.cpp:39: error: cannot convert from base 'IObject' to derived
type 'String' via virtual base 'IObject'
I just wanted to check that I'm using dynamic_cast correctly. I have a
hierarchy of objects using pure virtual classes and virtual inheritance
to implement interfaces. I ran into a problem using C-style casting to
an instance of a derived class from its interface type to a variable of
its concrete type. I was confused because I thought C-style casts
always compiled even if they wouldn't run correctly at runtime. I
modified the code to use dynamic_cast which seems correct, compiles and
runs but have found some contradictory information about its use so I
wanted to confirm I'm using it correctly. The code below illustrates
my problem. Thanks for your help.
#include <stdio.h>
class IObject
{
public:
virtual bool Equals(IObject *object) = 0;
};
class IList : public virtual IObject
{
public:
virtual IObject *Get(int index) = 0;
};
class Object : public virtual IObject
{
public:
virtual bool Equals(IObject *object) {
return this == object;
}
};
class ArrayList : public Object, public virtual IList
{
public:
virtual IObject *Get(int index) {
return this; // Just return something
}
};
class String : public Object { };
int main(int argc, char *argv[])
{
IList *list = new ArrayList();
IObject *obj = new String();
String *str = (String *)obj;
// This works: String *str = dynamic_cast<String *>(obj);
printf("str %s null.\n\0", str == 0 ? "is" : "is not");
}
Example.cpp: In function 'int main(int, char**)':
Example.cpp:39: error: cannot convert from base 'IObject' to derived
type 'String' via virtual base 'IObject'