By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!!!
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Dave wrote:
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).



Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.