从静态调用非静态方法的实例

An instance to call non-static methods from static

本文关键字:实例 静态方法 静态 调用      更新时间:2023-10-16

我在代码中发现了问题,但我不明白为什么它会这样工作。我正在尝试实现静态实例以从静态调用非静态方法。我真的需要它来用于我正在使用的 SDK 结构不佳的模块(橘子酱)。

收到橘子酱的错误,我正在尝试删除似乎正在使用的对象。但是如果我更改行:instance.http = new CIwHTTP();到这一行: http = new CIwHTTP();错误消失。

但是为什么它会这样工作呢?我认为实例 - 它是对我的类的引用,就像"this"变量一样。为什么这里有这么多不同?我认为"http."和"instance.http."之间没有任何区别,但是我怎样才能准确地对这个类进行"this"引用以在静态方法中使用它呢?

===[ HTTP.h ]===
#ifndef HTTP_H
#define HTTP_H
#include <s3e.h>
#include "IwHTTP.h"
class HTTP {
public:
    CIwHTTP *http;
    HTTP();
    ~HTTP();
};
#endif
===[ HTTP.cpp ]===
#include "HTTP.h"
static HTTP instance;
char* result = NULL;
HTTP::HTTP() {
    instance.http = new CIwHTTP();
}
HTTP::~HTTP() {
    if ( http ) {
        delete http;
    }
    s3eFree( result );
}

不要将"instance."放在 HTTP 构造函数中。否则,您永远不能有多个实例。