c++ - 为什么我们使用 'this->' 而不是 'this.' 来访问成员?

标签 c++ visual-c++

我在看一个人用 C++ 为 FaceBook 创建的库。头文件是这样的:

#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 joel@seligstein.com 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__

然后在cpp文件中,我的问题是关于这个,下面是构造函数:

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;
}

为什么他们使用 -> 运算符而不是 .

我对 -> 直接在内存中访问该类型的属性和方法的理解有限,但我很困惑,由于无知,我希望看到:

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;
}

我只想知道为什么 -> 在点符号上使用背后的理论。


更新: 对于和我在同一条船上学习 C++ 的其他人。我已经扩展了一个成员在此问题中发布的示例。我还包装了一个成员字段的初始化列表。

#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;
}

最佳答案

this 是指向当前对象的指针,即 A 类 的内部方法(或构造函数),thisA * 类型。

(请注意,如果该方法被标记为 const,则 this 的类型为 A const *。)

因此使用 ->(仅为指针设计)而不是 .(仅为类对象 A 或对类的引用设计对象 A&).

关于c++ - 为什么我们使用 'this->' 而不是 'this.' 来访问成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/680489/

相关文章:

c++ - 通过外部信号停止 std::thread 的有效方法是什么?

c++ - 在 C 中使用 pow、XOR 或指针时出现段错误

c++ - 为什么 Microsoft Visual C++ 库实现 'unwrap' 迭代器?

c++ - 如何从 Visual C++ 中的版本资源中读取

c++ - 链接到多线程调试 DLL 库时出现链接错误

android - 如何在不发疯的情况下使用 ARM DS-5 在 eclipse 中调试 Android native 库?

c++ - "T a"、 "T a()"和 "T a=T()"之间有什么区别,其中 T 是一个类?

c++ - 请求 ‘write’中的成员 ‘chol_out_data’,非类类型

c++ - 模板实例化的范围解析

c++ - VCRedist 2015 Update 1 向后兼容吗?