通用 var c++
我想创建一个通用变量,它可以来自一个类或另一个类。
在此代码示例中,我希望 var aa 是通用的,因此在我的代码中我可以访问 A 类或 B 类的代码。
但 aa 必须是全局的。
你可以帮帮我吗?
class MyClass
{
public:
MyClass() {} //contructor padrão, não deve ser utilizado isoladamente
static MyClass* getInstance()
{
static MyClass *instance = 0;
if (!instance) instance = new MyClass();
return instance;
}
};
class A : public MyClass
{
public:
int a() { return 1; }
};
class B : public MyClass
{
public:
int b() { return 1; }
};
template <class TAIT> class AIT
{
public:
static TAIT& Instance()
{
static TAIT instance;
return instance;
}
};
AIT aa;
void main()
{
aa.Instance().a(); // or
aa.Instance().b(); // aa could be class
// A or class B. So I could access
// function a or function b (not at same
// time, of course)
return;
}
I want to create a generic var, that could be from one class or another class.
In this code sample I want that var aa be generic so in my code I can access code from class A or class B.
But aa MUST BE GLOBAL.
Could you help me?
class MyClass
{
public:
MyClass() {} //contructor padrão, não deve ser utilizado isoladamente
static MyClass* getInstance()
{
static MyClass *instance = 0;
if (!instance) instance = new MyClass();
return instance;
}
};
class A : public MyClass
{
public:
int a() { return 1; }
};
class B : public MyClass
{
public:
int b() { return 1; }
};
template <class TAIT> class AIT
{
public:
static TAIT& Instance()
{
static TAIT instance;
return instance;
}
};
AIT aa;
void main()
{
aa.Instance().a(); // or
aa.Instance().b(); // aa could be class
// A or class B. So I could access
// function a or function b (not at same
// time, of course)
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简短的回答:
C++ 模板为您提供编译时通用性,而不是运行时通用性。
模板本身在您的程序中不存在(即作为具体的二进制可执行文件)。 仅当通过显式或隐式指定其参数进行实例化时,它们才会最终作为生成的代码。
因此,这不会声明一个变量:
这声明了一个变量:
如果您需要运行时通用性,则必须使用运行时调度机制:
Short answer:
C++ template give you compile-time genericity, not runtime genericity.
Templates by themselves have no existence in your program (that is as a concrete binary executable). They will end up as generated code only when instantiated by specifying their arguments either explicitly or implicitly.
So, this does NOT declare a variable:
This declares a variable:
If you need runtime genericity, you have to use a runtime dispatch mechanism:
如果我理解正确,我认为您只需要正确声明
aa
即可:如果这不是您的意思,请编辑您的问题并澄清您正在尝试的内容去做。
If I understand you correctly, I think that you just need to declare
aa
correctly, and you're set:If that isn't what you mean, please edit your question and clarify what you're trying to do.
在基类中声明纯虚函数,在派生类中重载它。
两个派生类都将具有同名的方法,您将根据对象类型自动解析要调用的方法。 您必须重写 GetInstance() 方法的创建部分,才能知道要创建哪个类实例。
Declare a pure virtual in the base class, overload it in the derived classes.
Both derived classes will have a method with the same name and you will resolve automatically which method to call depending on the object type. You will have to rewrite the creation part of the GetInstance() method for it to know what class instance to create.
如果您不希望在 getInstance 方法之外调用构造函数,则可以将其设为私有。 例如
现在
会导致编译错误,因为你的构造函数是私有的
If you don't want your constructor be called other than from in your getInstance method, you can make it private. e.g.
Now
Will cause a compile error because your constructor is private
这是解决这个问题的好方法吗?
Is this a good solution for this problem?
这绝对是令人讨厌的,但也许这里的解决方案是使用 void 指针。 在 C 和 C++ 中,声明为 void* 的指针被定义为没有类型。 您可以传递此指针,然后将其转换为您想要的任何数据类型的指针。 显然,非常仔细地确保变量实际上具有正确的类型至关重要,但是如果您正在设计一个不知道返回类型的接口,那么这可能很有用。 下面是接口类的示例:
然后,您可以创建另一个类来实现该接口并执行您想要的任何有趣的操作:
}
只要您拥有的任何代码了解要执行的操作,您就可以传回任何类型的指针你要。 如果您在 Google 中搜索 void 指针,将会得到更多信息。 当心。
It's definitely nasty, but perhaps the solution here is to use a void pointer. In C and C++, a pointer declared as void* is defined to have no type. You can pass this pointer around and then cast it to a pointer of whatever datatype you wish. Obviously it is crucial to very carefully ensure that the variable is actually of the right type, but if you are designing an interface where you don't know the return type, this can be useful. Here's an example of how the interface class might look:
You can then create another class that implements the interface and does whatever interesting things you want:
}
As long as any code you have understands what to do, you can pass back whatever type of pointer you want. There's plenty of more information if you search Google for void pointers. Be careful.