Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I pass the this pointer explicitly to a member function?

Tags:

c++

c++11

this

The c++ standard (ISO c++11) mentions in Section 9.3.1 that

A non-static member function may be called for an object of its class type, or for an object of a class derived (Clause 10) from its class type, using the class member access syntax (5.2.5, 13.3.1.1).

An attempt to compile this code with g++ (version 4.8.2)

 class foo{
    public:
         void bar(){
            cout<<"hey there"<<endl;
         }
};
int main(){
    foo obj;
    foo::bar(&obj);
}

gives a compile time error because it couldn't match the function's signature. I guess that is expected given what the standard states about calling member functions. Since the method will eventually take a form similar to bar(foo*) during some stage of compilation, why does the standard asks for member access syntax to call the member function?

like image 904
bashrc Avatar asked Oct 31 '14 04:10

bashrc


People also ask

When should I explicitly use a pointer to an object?

There are several reasons why you might need to use this pointer explicitly. When you want to pass a reference to your object to some function. When there is a locally declared object with the same name as the member object. When you're trying to access members of dependent base classes.

How to return a pointer from a member function?

2) No Alternative:To return a pointer or reference to thisfrom a member function. This is frequently done (and should be done) when overloading operator+, operator-, operator=, etc: class Foo { Foo& operator=(const Foo& rhs) { return * this; } };

Why static function does not understand this pointer inside its body?

But, static function of a class is not associated with class object. So, THIS pointer is not passed to a static function as an internal parameter. So, a static function does not understand THIS pointer inside its body. If we compile below class, compiler with throw an error i.e. static functions do not have this pointer.

Can pass by reference and pass by value be done simultaneously?

Pass by reference and pass by value can’t be done simultaneously in a single function argument list. What is the size of object pointer? Assigning objects takes place while passing the arguments. Why temporary object is not created in return by reference?


1 Answers

Lets add a static member to the class as:

 class foo{
    public:
         void bar()     { cout<<"hey there"<<endl; }
         static void bar(foo*) { cout<<"STATIC MEMBER"<<endl; }
};

Now if you write this:

 foo::bar(&obj); //static or non-static?

Which function should be called? In such situation, how would you call both of them? What would be the syntax? If you allow one function to have this syntax, you've to abandon it (i.e syntax) for other function. The Standard decided to have foo::bar(&obj) syntax for static member function, while abandoning it for non-static member function.


Anyway, if you want to pass &obj as argument to the non-static member function, then you can use type-erasure facilitated by std::function as:

 void (foo::*pbar)() = &foo::bar; //non-static member function   #1

 std::function<void(foo*)> bar(pbar); 

 bar(&obj); //same as obj.bar();

Likewise, you could call static member function as:

 void (*pbar)(foo*) = &foo::bar; //static member function            #2

 std::function<void(foo*)> bar(pbar); 

 bar(&obj); //same as foo::bar(&obj);

Note that at lines #1 and #2, the types of the object pbar makes the compiler to choose the correct member function — in the first case, it takes the pointer to the non-static member-function while in the latter case, it takes the pointer to the static member function.

Hope that helps.

like image 70
Nawaz Avatar answered Oct 03 '22 21:10

Nawaz