tags:

views:

369

answers:

6

Hi,

I was looking at a library a person has made for FaceBook in C++. The header file is this:

#ifndef __FACEBOOK_H__
#define __FACEBOOK_H__

/**
 * Facebook Class 
 * Joel Seligstein
 * Last mod: Aug 22, 2006
 *
 * This is the beginnings of a facebook class set and REST client.  Its not documented
 * yet nor nearly complete.  But this is a release to demonstrate its usefulness.  
 * Please email [email protected] with suggestions or additions.
 *
 * TODO: Create classes/parsers for each request type
 * TODO: Linux URL launcher
 */

//uncomment to have verbose output turned on
//#define fb_debug 1

//define which platform you're compiling for
#define fb_windows 1
//#define fb_linux 1

#include <string>
#include <sstream>
#include <list>
using namespace std;

#ifdef fb_windows
#include <windows.h>
#endif

#include "curl/curl.h"
#include "xmlParser/xmlParser.h"
#include "md5.h"

class facebook
{
    public:
     //app/session vars
     string api_key;
     string secret;
     string token;
     string server;
     string session_key;
     string session_secret;
     string uid;
     bool has_session;

     facebook( string my_key, string my_secret, string my_server );
     bool authenticate( );
     bool request( string method, list<string> params, string *res );
     bool load_token( );
     void launch_login( string url );
     bool get_session( );
     void clean_up( );

    private:
     //curl info
     CURL *curl;
     CURLcode res;
     int call_id;

     //internal functions
     string get_signature( list<string> params );
     static string md5( string str );
     static string get_param_string( list<string> params, bool separate );
     static size_t write_callback( void *ptr, size_t size, size_t nmemb, void *userp );
};

#endif //__FACEBOOK_H__

Then inside the cpp file, my question is regarding this, the below is the constructor:

facebook::facebook( string my_key, string my_secret, string my_server )
{
    this->api_key = my_key;
    this->secret = my_secret;
    this->server = my_server;
    this->has_session = false;
    this->call_id = 0;
}

Why have they used the -> operator and not .

I have a limited understanding that -> accesses properties and methods of the type directly in memory but I am confused, I would, through ignorance, expect to see:

facebook::facebook( string my_key, string my_secret, string my_server )
{
    this.api_key = my_key;
    this.secret = my_secret;
    this.server = my_server;
    this.has_session = false;
    this.call_id = 0;
}

All i want to know is the theory behind why -> is used over the dot notation. Thanks very much,

Andre

UPDATE: For anyone else in the same boat as me and learning C++. I have expanded on an example posted by a member in this question. I have also wrapped in initalization list for a member field.

Thanks to everyone for their contribution. I appreciate your time.

#include "stdafx.h"
#include <iostream>
using namespace std;

class A {
private:
    int x;
public:
    A() : x(0){}
    int getX() const {return x;}
    void setX(int xx) {x += xx;}
};

int main()
{
    A a;

    a.setX(13);

    A *pa = &a;

    pa->setX(2);

    A b = a;

    b.setX(5);

    cout << "a" << a.getX() << endl;

    cout << "a*" << pa->getX() << endl;

    cout << "b" << b.getX() << endl;

    return 0;
}
+6  A: 

"this" is a pointer to the current object ie, inside methods (or constructor) of class A, this is of type "A *". Note that, if the method is tagged as const, this is of type "A const *". Hence the use of "->" (designed of pointers) and not "." (designed for class objects "A" or references to class objects "A&")

Benoît
Thanks for the answer. In what situation would the dot notation be used?
REA_ANDREW
if you don't have got a pointer object but a reference e.g. string b = "asdfghjklö"; b = b.substr(0,5); this is calling the substr function of the string object b
bernhardrusch
+2  A: 

In C++, this is a pointer to the self object. It's a relic leftover from early versions of C++, before it had references. If this were added to the language now, it probably would be a reference, for which you'd use the . notation.

Jesse Beder
There is no "this" in C. But first versions of C++ did not have references, if i remember correctly.
Benoît
Whoops, sounds right
Jesse Beder
For the reason why 'this' is not a reference you can check this question: http://stackoverflow.com/questions/645994/why-this-is-a-pointer-and-not-a-reference
Naveen
+2  A: 

Because inside a class or struct member, "this" is a special pointer that points to the current instance.

Any time you're accessing members of a class or struct through a pointer, the -> operator is used.

If you're accessing them through anything that isn't a pointer (usually a reference), the dot operator is used instead.

It's worth noting that the this-> syntax isn't necessary except in some forms of template instanciation. A lot of people choose to use it for stylistic reasons, though. There are a couple questions related to rationale behind this on S.O. already.

Dan Olson
+1  A: 

As said, this is a pointer and not a reference. Thus you need the -> operator that is roughly (while not exactly the same as): (*this).

They differ in that you can overload -> operator or * operator in a class and can have different semantics. As a matter of fact, if a class overrides operator-> and the returned value is not a raw pointer, the compiler will keep on applying operator-> to the returned object until it reaches a native pointer. Operator . cannot be overloaded.

In the constructor you pointed, the use of this is completely optional as there is no name collision the code could just have named the attributes without further qualification.

facebook::facebook( string my_key, string my_secret, string my_server )
{
    api_key = my_key;
    secret = my_secret;
    server = my_server;
    has_session = false;
    call_id = 0;
}

And a better solution would be using an initialization list. If you are interested in learning C++ google for it.

David Rodríguez - dribeas
Thanks I just did google that. In c# we use the base keyword for intializing a parent type contructor with arguments. I am very interested in the fact fields can be initialized in the same way with an initialization list. Thanks for the comment! :-)
REA_ANDREW
Initialization lists are not only for calling bases constructors, but is where the attributes are initialized. Inside the {} the attributes have already been default initialized and you are rewriting them. Initialization lists are a must with references as they cannot be default initialized.
David Rodríguez - dribeas
+2  A: 

You may consider the following example.

class A {
    int x;
public:
    int getX() const {return x;}
    void setx(int xx) {x = xx;}
};

int main()
{
    A a;

    a.setX(13);
    cout << a.getX() << endl;

    A *pa = &a;

    cout << pa->getX() << endl;

    return 0;
}

Notice that, dot operator (.) is used when calling a member function of A directly with the object a. The arrow operator (->) is used when calling a member function of A indirectly through a pointer of type A (A *pa = &a).

In C++, this is a pointer to the invoking object. Thus we could also define A::getX() function as follows:

int getX() const {return this->x;}

Or:

int getX() const {return (*this).x;}

this is a pointer of object A, thus *this dereferences it. So we could use both of the functions above.

Donotalo
Thanks I used your example and expanded it a little whilst understanding it. I also wrapped in initialization list feature. Cheers again. +1;
REA_ANDREW
+1  A: 

See this question: why-this-is-a-pointer

Jimmy J