((Object *)NULL)->MemberFunction();

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
 
I

Ian Collins

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.
 
K

Kai-Uwe Bux

Ian said:
Drop the C style void,

virtual const char* GetID() const = 0;

Is correct and idiomatic C++.

Well,

virtual const char * GetID (void) const = 0;

is correct C++, too.

This can not work for polymorphic objects. The object has to be
properly constructed in order to call a virtual method.

The code dereferences a null pointer. It has undefined behavior regardless
of whether the method is virtual or not and of whether the the "object"
(which does not exist) is polymorphic or not.


Best

Kai-Uwe Bux
 
I

Ian Collins

Kai-Uwe Bux said:
Well,

virtual const char * GetID (void) const = 0;

is correct C++, too.
If not idiomatic. The void serves a purpose in C, but not in C++.
The code dereferences a null pointer. It has undefined behavior regardless
of whether the method is virtual or not and of whether the the "object"
(which does not exist) is polymorphic or not.
True, but it falls into the "probably works" box for normal member
functions that don't access any data members. But yes, it is a bad
thing to do.
 
S

Sam

Ian said:
If not idiomatic. The void serves a purpose in C, but not in C++.

True, but it falls into the "probably works" box for normal member
functions that don't access any data members. But yes, it is a bad
thing to do.

It will /never/ work. Why? Because GetID() is a virtual function. Think
about it.



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHyh+Qx9p3GYHlUOIRAo5EAJ0cSMYV5hyYYJJhB6sRJFExjDjkkQCfXJOu
/zFSSi1guB/uIyPiJDqAaWc=
=xG8Z
-----END PGP SIGNATURE-----
 
J

jason.cipriani

If not idiomatic. The void serves a purpose in C, but not in C++.

Cut it out, you two. I've only just started kicking the habit; that
(void) slipped by me. :p
True, but it falls into the "probably works" box for normal member
functions that don't access any data members. But yes, it is a bad
thing to do.

Ok, thanks for clearing that up, guys.

Jason
 
J

James Kanze

Drop the C style void,
virtual const char* GetID() const = 0;
Is correct and idiomatic C++.
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.
 
J

James Kanze

virtual const char * GetID (void) const = 0;
is correct C++, too.

Yes, but that's just a hack in the standard, added for reasons
of C compatibility. (It wasn't legal in older versions of C++,
before the C committee created this monstrosity.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top