rahman
rahman

Reputation: 4948

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. 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;

the code would compile just fine. Why?

EDIT:

Thanks for all the answers, but, if I modify C C::instances; to C C::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 toA::A(int, int)' collect2: ld returned 1 exit status

any idea why? and how to fix it?

thanks

Upvotes: 0

Views: 495

Answers (4)

Sanish
Sanish

Reputation: 1729

C C::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
{
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.

Upvotes: 1

Frerich Raabe
Frerich Raabe

Reputation: 94549

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 (i.e. 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, e.g.

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

On your second question: a std::vector has a default constructor.

Upvotes: 1

lx.
lx.

Reputation: 2327

With C C::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. So you'd have to create instaces either with 2 parameters C C::instances(0, 0) or alternatively provide an additional default constructor for C:

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

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258698

If you define a constructor, the compiler no longer generates a default one for you, which you attempt to call with C C::instances;. You can bypass this by calling the available constructor:

C C::instances(0,0);

or provide a default constructor for 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. But

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

wouldn't compile.

Upvotes: 4

Related Questions