简体   繁体   English

在C ++中继承的单例

[英]Inherited singleton in C++

Could you please check out this piece of code: 您能否检查一下这段代码:

#include <vector>
class A
{
public:
    A(int a, int b);
};

class C :public A
{
public:
    C(int a, int b):A(a,b){}
    static C instances;
};

C C::instances;

int main()
{
    return 1;
}

The compilation gives me error as: 编译给我的错误是:

$ c++ inheritance.cpp 
inheritance.cpp:16:6: error: no matching function for call to ‘C::C()’
inheritance.cpp:16:6: note: candidates are:
inheritance.cpp:12:2: note: C::C(int, int)
inheritance.cpp:12:2: note:   candidate expects 2 arguments, 0 provided
inheritance.cpp:8:7: note: C::C(const C&)
inheritance.cpp:8:7: note:   candidate expects 1 argument, 0 provided

I need C to inherit from A and I need A to have arguments in its constructor. 我需要C继承自A,并且我需要A在其构造函数中具有参数。 Finally, I need the instance's static variable to be declared and defined with no arguments. 最后,我需要在没有参数的情况下声明和定义实例的静态变量。 So is there a solution to that? 那么有解决方案吗? I value your kind comments. 我重视您的评论。

Another point to note: if the static variable was a container, like: 需要注意的另一点:如果静态变量是容器,例如:

static std::vector instances; 静态std :: vector实例;

the code would compile just fine. 该代码将编译就好了。 Why? 为什么?

EDIT: 编辑:

Thanks for all the answers, but, if I modify CC::instances; 感谢您提供所有答案,但是,如果我修改CC::instances; to CC::instances(0,0); CC::instances(0,0); i will get another error: $ c++ inheritance.cpp /tmp/cctw6l67.o: In function C::C(int, int)': inheritance.cpp:(.text._ZN1CC2Eii[_ZN1CC5Eii]+0x1b): undefined reference to A::A(int, int)' collect2: ld returned 1 exit status 我将收到另一个错误:$ c ++ Inheritance.cpp /tmp/cctw6l67.o:在函数C::C(int, int)': inheritance.cpp:(.text._ZN1CC2Eii[_ZN1CC5Eii]+0x1b): undefined reference to A :: A(int,int)'collect2:ld返回1退出状态

any idea why? 知道为什么吗? and how to fix it? 以及如何解决?

thanks 谢谢

If you define a constructor, the compiler no longer generates a default one for you, which you attempt to call with CC::instances; 如果定义了构造函数,则编译器将不再为您生成默认的构造函数,而是尝试使用CC::instances;进行调用CC::instances; . You can bypass this by calling the available constructor: 您可以通过调用可用的构造函数来绕过此操作:

C C::instances(0,0);

or provide a default constructor for C . 或为C提供默认的构造函数。

With

static std::vector<C> instances;

it compiles because no elements are created, and std::vector has a default constructor which initializes an empty vector. 它会编译,因为没有创建任何元素,并且std::vector具有默认构造函数,该构造函数初始化了一个空向量。 But

C::instances.push_back(C());

wouldn't compile. 不会编译。

With CC::instances; 使用CC::instances; you are constructing an object, therefore calling its constructor. 您正在构造一个对象,因此调用其构造函数。

Since you provided a constructor with two arguments C(int a, int b) , the default constructor (which requires none) is not generated automatically for you any more. 由于您为构造函数提供了两个参数C(int a, int b) ,因此不会自动为您自动生成默认的构造函数(不需要)。 So you'd have to create instaces either with 2 parameters CC::instances(0, 0) or alternatively provide an additional default constructor for C: 因此,您必须使用2个参数CC::instances(0, 0)创建CC::instances(0, 0)或者为C提供额外的默认构造函数:

C() : A(0, 0)
{
}

On your first question: you're getting this error since the line 关于第一个问题:自上一行以来,您正在收到此错误

C C::instances;

attempts to call a C constructor which takes no argument (ie C::C() ). 尝试调用不带参数的C构造函数(即C::C() )。 You could either fix this by introducing a C::C() constructor which calls A::A(int, int) with two default values, or you specify default values for the existing constructor, eg 您可以通过引入C::C()构造函数A::A(int, int)使用两个默认值调用A::A(int, int)来解决此问题,也可以为现有构造函数指定默认值,例如

C::C(int a = 0, int b = 0) : A(a, b) {}

On your second question: a std::vector has a default constructor. 关于第二个问题: std::vector具有默认构造函数。

CC::instances;

This would try to call the default constructor and compiler will not provide one since you provided a constructor. 这将尝试调用默认构造函数,并且由于您提供了构造函数,因此编译器将不提供该构造函数。

Add default constructor to class A and class C 将默认构造函数添加到class A class Cclass C

class A
{
public:
   A(int a, int b);
   A() {}  // implement default constructor
 };

//class C
 class C :public A
 {
 public :

    C(int a, int b):A(a,b){}
     C() {} // default constructor
     static C instances;
  };

static std::vector instances;

This will work because std::vector has a default constructor. 这将起作用,因为std::vector具有默认的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM