Re: ((Object *)NULL)->MemberFunction();
On 2 mar, 04:06, Ian Collins <ian-n...@hotmail.com> wrote:
jason.cipri...@gmail.com wrote:
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;
Drop the C style void,
virtual const char* GetID() const = 0;
Is correct and idiomatic C++.
// 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();
}
This can not work for polymorphic objects. The object has to
be properly constructed in order to call a virtual method.
It's undefined behavior for any member function, virtual or not.
The fact that in some implementations, it does seem to work if
the function isn't virtual is irrelevant.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34