前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >this指针如何使C++成员指针可调用

this指针如何使C++成员指针可调用

原创
作者头像
jackcode
发布2024-06-12 10:59:37
710
发布2024-06-12 10:59:37
举报
文章被收录于专栏:爬虫资料爬虫资料
爬虫代理
爬虫代理

在C++中,this指针是一个隐藏的指针,指向当前对象实例。它在成员函数中自动可用,用于访问该对象的成员变量和成员函数。理解this指针的工作原理有助于理解为什么指向成员的指针是可调用的。在本文中,我们将详细探讨this指针的概念,并通过具体的代码示例说明其在指向成员指针中的应用。

1. 理解this指针

每个非静态成员函数都有一个隐藏的this指针,指向调用该函数的对象。this指针在成员函数内部自动可用,允许函数访问对象的成员。

代码语言:cpp
复制
class MyClass {
public:
    int value;
    void display() {
        std::cout << "Value: " << this->value << std::endl;
    }
};

在上述代码中,this->value访问了当前对象的成员变量value

2. 指向成员变量的指针

指向成员变量的指针用于指向类的成员变量。这种指针不能单独使用,必须结合特定的对象实例。

代码语言:cpp
复制
class MyClass {
public:
    int value;
};

int MyClass::*ptr = &MyClass::value;

MyClass obj;
obj.value = 42;

// 使用成员变量指针访问对象成员
std::cout << "Value: " << obj.*ptr << std::endl;

在这里,ptr是指向成员变量value的指针,必须通过对象实例obj来访问。

3. 指向成员函数的指针

指向成员函数的指针用于指向类的成员函数。这种指针的类型为ReturnType (ClassName::*)(ArgType),其中ReturnType是函数的返回类型,ArgType是函数的参数类型。

代码语言:cpp
复制
class MyClass {
public:
    void display(int num) {
        std::cout << "Number: " << num << std::endl;
    }
};

void (MyClass::*funcPtr)(int) = &MyClass::display;

MyClass obj;
(obj.*funcPtr)(42);

在这里,funcPtr是指向成员函数display的指针,通过对象实例obj调用。

4. this指针如何使成员指针可调用

在调用成员函数指针时,编译器隐式地传递了this指针,使得成员函数可以访问当前对象的成员变量。这是为什么成员指针可以通过对象实例调用的原因。

5. 代码示例:使用指针的爬虫示例

我们将实现一个多线程的爬虫示例,使用代理IP技术来提高采集效率。我们假设使用爬虫代理,其域名、端口、用户名和密码分别为proxy.host.cn8080usernamepassword。在示例中,我们将利用C++的成员指针和this指针。

爬虫类设计

首先,我们设计一个爬虫类,其中包括成员函数和指向这些成员的指针。

代码语言:cpp
复制
#include <iostream>
#include <thread>
#include <vector>
#include <curl/curl.h>

//爬虫代理设置(使用亿牛云爬虫代理加强版)
const std::string PROXY = "proxy.host.cn:8080";
const std::string PROXY_USERPWD = "username:password";

// 用于接收curl回调的函数
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

class WebCrawler {
public:
    // 成员变量
    std::string response;

    // 成员函数
    void fetchUrl(const std::string& url) {
        CURL* curl;
        CURLcode res;
        response.clear();

        curl = curl_easy_init();
        if (curl) {
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
            curl_easy_setopt(curl, CURLOPT_PROXY, PROXY.c_str());
            curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, PROXY_USERPWD.c_str());
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
            res = curl_easy_perform(curl);
            if (res != CURLE_OK) {
                std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
            } else {
                std::cout << "Data from " << url << ": " << response << std::endl;
            }
            curl_easy_cleanup(curl);
        }
    }

    // 使用成员函数指针调用
    void startMultithreadedCrawl(const std::vector<std::string>& urls) {
        std::vector<std::thread> threads;
        void (WebCrawler::*funcPtr)(const std::string&) = &WebCrawler::fetchUrl;

        for (const auto& url : urls) {
            threads.emplace_back(std::thread(funcPtr, this, url));
        }
        for (auto& thread : threads) {
            if (thread.joinable()) {
                thread.join();
            }
        }
    }
};

int main() {
    WebCrawler crawler;
    std::vector<std::string> urls = {
        "http://example.com",
        "http://example.org",
        "http://example.net"
    };
    crawler.startMultithreadedCrawl(urls);
    return 0;
}

代码解释

  1. WebCrawler类
  2. 包含一个成员变量response用于存储HTTP响应。
  3. 包含一个成员函数fetchUrl,用于使用CURL库从给定URL抓取数据。
  4. 包含一个成员函数startMultithreadedCrawl,用于启动多线程爬取。
  5. 成员函数指针
  6. void (WebCrawler::*funcPtr)(const std::string&) = &WebCrawler::fetchUrl; 定义了一个指向成员函数fetchUrl的指针。
  7. std::thread(funcPtr, this, url) 通过this指针将成员函数指针与具体的对象实例绑定,在多线程中调用。
  8. 多线程爬取
  9. 创建一个包含多个URL的向量。
  10. 调用startMultithreadedCrawl函数,在每个线程中通过成员函数指针调用fetchUrl

通过以上示例,我们不仅展示了this指针如何使C成员指针可调用,还展示了在实际应用中如何结合代理IP技术和多线程技术提高爬取效率。希望这些内容能帮助您更好地理解C中的this指针和成员指针的应用。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 理解this指针
  • 2. 指向成员变量的指针
  • 3. 指向成员函数的指针
  • 4. this指针如何使成员指针可调用
  • 5. 代码示例:使用指针的爬虫示例
    • 爬虫类设计
      • 代码解释
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档