Calling non-static member functions without an object?????

D

Dave

In Exceptional C++, the following code snippet appears at the bottom of page
197:

template<class T>
class Array : private ArrayBase, public Container
{
typedef Array AIType;
public:
Array( size_t startingSize = 10 )
: Container( startingSize ),
ArrayBase( Container::GetType() ),

After this code snippet, the following paragraph appears:

1. If GetType() is a static member function, or a member function that does
not use its this pointer (that is, uses no member data) and does not rely on
any side effects of construction (for example, static usage counts), then
this is merely poor style, but it will run correctly.


What am I missing here? Since when can a non-static member function ever be
called except through a concrete object? Container::GetType() is not a
valid call if GetType() is not static!!!! Surely I must be missing
something incredibly obvious here! Somebody, please set me straight!!!
 
G

Gianni Mariani

Dave said:
In Exceptional C++, the following code snippet appears at the bottom of page
197:

template<class T>
class Array : private ArrayBase, public Container
{
typedef Array AIType;
public:
Array( size_t startingSize = 10 )
: Container( startingSize ),
ArrayBase( Container::GetType() ),

Container::GetType() >>> this refers to the inherited Container object

After this code snippet, the following paragraph appears:

1. If GetType() is a static member function, or a member function that does
not use its this pointer (that is, uses no member data) and does not rely on
any side effects of construction (for example, static usage counts), then
this is merely poor style, but it will run correctly.

Which inherited object gets constructed first ? The ArrayBase or the
Container ? The point is that Container::GetType() gets called before
the Container object is constructed - hence the assertion that of the
undefined nature of the GetType() call. However, I'd say that the
result is plain undefined and you'd be far better off using a static
member function.

What am I missing here? Since when can a non-static member function ever be
called except through a concrete object? Container::GetType() is not a
valid call if GetType() is not static!!!! Surely I must be missing
something incredibly obvious here! Somebody, please set me straight!!!

Another example - which is what I originally thought you were thinking :

class FooBar
{
public:
void Method()
{}
};

int main()
{
FooBar * x = 0;

x->Method();
//^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined behaviour
}

Even though this is undefined, it will run quite happily on most
platforms - BUT it's really just plain wrong and it's not worth the risk
(as it is for any undefined behaviour).
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top