通用 var c++

发布于 2024-07-25 08:35:48 字数 981 浏览 8 评论 0原文

我想创建一个通用变量,它可以来自一个类或另一个类。

在此代码示例中,我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情话已封尘 2024-08-01 08:35:48

简短的回答:

C++ 模板为您提供编译时通用性,而不是运行时通用性。

模板本身在您的程序中不存在(即作为具体的二进制可执行文件)。 仅当通过显式或隐式指定其参数进行实例化时,它们才会最终作为生成的代码。

因此,这不会声明一个变量:

AIT aa;

这声明了一个变量:

AIT<A> aa;

如果您需要运行时通用性,则必须使用运行时调度机制:

  • 要么使用 switch case 手动执行。
  • 或者更好地使用公共基类的内置继承+一些虚拟方法。

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:

AIT aa;

This declares a variable:

AIT<A> aa;

If you need runtime genericity, you have to use a runtime dispatch mechanism:

  • either do it manually with a switch case.
  • or better use the builtin inheritance of a common base class + some virtual methods.
誰ツ都不明白 2024-08-01 08:35:48

如果我理解正确,我认为您只需要正确声明 aa 即可:

AIT<A> aa;
aa.Instance().a();

AIT<B> bb;
bb.Instance().b();

如果这不是您的意思,请编辑您的问题并澄清您正在尝试的内容去做。

If I understand you correctly, I think that you just need to declare aa correctly, and you're set:

AIT<A> aa;
aa.Instance().a();

AIT<B> bb;
bb.Instance().b();

If that isn't what you mean, please edit your question and clarify what you're trying to do.

送舟行 2024-08-01 08:35:48

在基类中声明纯虚函数,在派生类中重载它。

class MyClass {
public:
   virtual int a() = 0;
};

class DerivedA : public MyClass {
public:
   virtual int a() { return 1; }
};

两个派生类都将具有同名的方法,您将根据对象类型自动解析要调用的方法。 您必须重写 GetInstance() 方法的创建部分,才能知道要创建哪个类实例。

Declare a pure virtual in the base class, overload it in the derived classes.

class MyClass {
public:
   virtual int a() = 0;
};

class DerivedA : public MyClass {
public:
   virtual int a() { return 1; }
};

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.

深陷 2024-08-01 08:35:48

如果您不希望在 getInstance 方法之外调用构造函数,则可以将其设为私有。 例如

class MyClass
{
private:
    MyClass() {} //contructor padrão, não deve ser utilizado isoladamente
public:
    static MyClass* getInstance()
    {
        static MyClass *instance = 0;
        if (!instance) instance = new MyClass();
            return instance;
    }
};

现在

MyClass* a = new MyClass();

会导致编译错误,因为你的构造函数是私有的

If you don't want your constructor be called other than from in your getInstance method, you can make it private. e.g.

class MyClass
{
private:
    MyClass() {} //contructor padrão, não deve ser utilizado isoladamente
public:
    static MyClass* getInstance()
    {
        static MyClass *instance = 0;
        if (!instance) instance = new MyClass();
            return instance;
    }
};

Now

MyClass* a = new MyClass();

Will cause a compile error because your constructor is private

场罚期间 2024-08-01 08:35:48

这是解决这个问题的好方法吗?

类我的类{
民众:
MyClass() {} //构造函数,不可用
伊索拉达门特
虚拟 int a() = 0; 虚拟 int b() = 0;

 int c() { return b();   } 

     静态 MyClass* getInstance() 
     { 
        静态 MyClass *实例 = 0;   // if (!instance) 实例 
  

= new MyClass();
返回实例;
} };

A 类:公共 MyClass { 公共:
int a() { 返回 1; } int b() {
返回1; } int d() { 返回 1; } };

B 类:公共 MyClass { 公共:
int a() { 返回 1; } int b() {
返回1; } int e() { 返回 1; } };

C 类:公共 A,公共 B { 公共:
一个ca; B cb;

枚举 TIPO { A, B }; TIPO 蒂波;

C(TIPO 蒂波) { Tipo = 蒂波; }

int a() { switch(Tipo) { case
答:返回ca.a(); 休息; 案件
B:返回cb.b(); 休息;
默认:中断; } }

};

void main() { //MyClass* A =
MyClass::getInstance(); //我的班级* B
= MyClass::getInstance();

C c(C::B); ca();
// aa.Instance().a();
// aa.Instance().b();

返回; }

Is this a good solution for this problem?

class MyClass {
public:
MyClass() {} //contructor padrão, não deve ser utilizado
isoladamente
virtual int a() = 0; virtual int b() = 0;

  int c()         {           return b();         }

   static MyClass* getInstance()
   {
      static MyClass *instance = 0; //          if (!instance) instance

= new MyClass();
return instance;
} };

class A : public MyClass { public:
int a() { return 1; } int b() {
return 1; } int d() { return 1; } };

class B : public MyClass { public:
int a() { return 1; } int b() {
return 1; } int e() { return 1; } };

class C : public A, public B { public:
A ca; B cb;

enum TIPO { A, B }; TIPO Tipo;

C(TIPO tipo) { Tipo = tipo; }

int a() { switch(Tipo) { case
A: return ca.a(); break; case
B: return cb.b(); break;
default: break; } }

};

void main() { //MyClass* A =
MyClass::getInstance(); //MyClass* B
= MyClass::getInstance();

C c(C::B); c.a();
// aa.Instance().a();
// aa.Instance().b();

return; }

我们只是彼此的过ke 2024-08-01 08:35:48

这绝对是令人讨厌的,但也许这里的解决方案是使用 void 指针。 在 C 和 C++ 中,声明为 void* 的指针被定义为没有类型。 您可以传递此指针,然后将其转换为您想要的任何数据类型的指针。 显然,非常仔细地确保变量实际上具有正确的类型至关重要,但是如果您正在设计一个不知道返回类型的接口,那么这可能很有用。 下面是接口类的示例:

class InterfaceClass {
  public:
     virtual void* someInterface() = 0;
}

然后,您可以创建另一个类来实现该接口并执行您想要的任何有趣的操作:

class DerivedClass: public InterfaceClass {
  public:
     virtual void* someInterface() {
        //do some stuff
        return (void*) somePointer;

}

只要您拥有的任何代码了解要执行的操作,您就可以传回任何类型的指针你要。 如果您在 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:

class InterfaceClass {
  public:
     virtual void* someInterface() = 0;
}

You can then create another class that implements the interface and does whatever interesting things you want:

class DerivedClass: public InterfaceClass {
  public:
     virtual void* someInterface() {
        //do some stuff
        return (void*) somePointer;

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文