J
jason.cipriani
Sorry about the subject but it's the most descriptive thing I came up
with. Is something like this safe to do:
// all objects let you get some string ID
class BaseObject {
public:
virtual const char * GetID (void) const;
};
// here's an example of one of the "objects":
class SomeObject : public BaseObject {
public:
const char * GetID (void) const { return "SomeID"; }
};
// i can guarantee that no GetID implementations access any
// member data; they all just return a char array, nothing more.
// then, this function gets the ID of an object given it's type,
// without actually instantiating a T:
template <class T> const char * GetObjectID () {
return ((T *)NULL)->GetID();
}
// and here i can use GetObjectID, or I can also get the ID of
// an existing instance:
void something () {
...... = GetObjectID<SomeObject>();
BaseObject *obj = ........;
...... = obj->GetID();
}
The goal there is to have a "GetID" member function that is part of
the base interface, so that it can be called on actual BaseObject's;
but at the same time use the same "GetID" member function without
having to instantiate anything. It's supposed to be a cross between a
non-static base member function, and a static member function like if
I did (which requires no instance but isn't part of a base interface):
class SomeObject {
public:
static const char * Something (void) { return "Something"; }
};
template <class T> GetSomething (void) {
return T::Something();
}
So the question is, if I call a virtual member function on a NULL
object, is that well-defined (and this == NULL, so as long as I don't
access any member data), or is it possible that it will lead to weird
things happening?
Thanks,
Jason
with. Is something like this safe to do:
// all objects let you get some string ID
class BaseObject {
public:
virtual const char * GetID (void) const;
};
// here's an example of one of the "objects":
class SomeObject : public BaseObject {
public:
const char * GetID (void) const { return "SomeID"; }
};
// i can guarantee that no GetID implementations access any
// member data; they all just return a char array, nothing more.
// then, this function gets the ID of an object given it's type,
// without actually instantiating a T:
template <class T> const char * GetObjectID () {
return ((T *)NULL)->GetID();
}
// and here i can use GetObjectID, or I can also get the ID of
// an existing instance:
void something () {
...... = GetObjectID<SomeObject>();
BaseObject *obj = ........;
...... = obj->GetID();
}
The goal there is to have a "GetID" member function that is part of
the base interface, so that it can be called on actual BaseObject's;
but at the same time use the same "GetID" member function without
having to instantiate anything. It's supposed to be a cross between a
non-static base member function, and a static member function like if
I did (which requires no instance but isn't part of a base interface):
class SomeObject {
public:
static const char * Something (void) { return "Something"; }
};
template <class T> GetSomething (void) {
return T::Something();
}
So the question is, if I call a virtual member function on a NULL
object, is that well-defined (and this == NULL, so as long as I don't
access any member data), or is it possible that it will lead to weird
things happening?
Thanks,
Jason