从静态调用非静态方法的实例
An instance to call non-static methods from static
我在代码中发现了问题,但我不明白为什么它会这样工作。我正在尝试实现静态实例以从静态调用非静态方法。我真的需要它来用于我正在使用的 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 构造函数中。否则,您永远不能有多个实例。
相关文章:
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 如何通过命名空间调用非静态方法
- 如何在没有实例的情况下获取非静态方法的类型?
- 调用从模板派生的类的静态方法,而不指定模板
- 如何在 c++ 中异步调用静态方法?
- C++ 将静态方法转换为简单方法
- 从另一个标头中的标头调用静态方法
- C++中静态方法的局部变量范围
- 如何启用使用另一个类的静态方法的模板函数的自动推导,从而消除冗长的调用方代码
- 使用本机 JNI 静态方法实现C++ Java 运行时错误
- 当只有静态方法受到影响时,如何解决C++中的链接器错误?
- 在不指定实例化的情况下调用类模板的静态方法的方法
- 从指向实例的指针调用静态方法时出现链接错误
- Boost::p ython 静态方法返回类的实例
- 设置为实例的静态值在从静态方法设置时有效,但不能从类内的静态方法设置
- 将参数中的静态函数指针替换为实例中方法的指针
- 在同一个c#或c++类中可以有静态方法和实例方法吗?
- 如何在静态方法中实例化当前类的实例
- 静态方法类实例化
- 从静态调用非静态方法的实例