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

Discussion in 'C++' started by Dave, Nov 19, 2003.

  1. Dave

    Dave Guest

    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!!!
     
    Dave, Nov 19, 2003
    #1
    1. Advertising

  2. 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).


    >
     
    Gianni Mariani, Nov 19, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nagesh
    Replies:
    3
    Views:
    741
    Nagesh
    Dec 26, 2005
  2. Replies:
    9
    Views:
    956
    Ben Bacarisse
    Feb 6, 2006
  3. dolphin
    Replies:
    3
    Views:
    1,350
    Pete Becker
    Dec 5, 2007
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page