Problem ze zrozumieniem zmiennej statycznej.

0
#include <iostream>
using namespace std;

class A
{
public:
	A() : a(0)
	{
		cout << "Construktor A: a=" << a << endl;
	}
	~A()
	{
		cout<< " Destruktor A"<<endl;
	}
		int a;
};
class B : public A
{
public:
	B() : b(1)
	{
		cout << "Constructor B: b=" << b << endl;

	}
	~B()
	{
		cout << "Destructor B" << endl;
	}
	int b;
};


class C : public A
{
public:
	C() : c(2)
	{
		cout << "Constructor C: c=" << c << endl;

	}
	~C()
	{
		cout << "Destructor C" << endl;
	}
	int c;
};

class E
{
public:
	E() : e(4)
	{
		cout << "Constructor E: e=" << e << endl;
	}
	~E()
	{
		cout << "Destructor E" << endl;
	}
		int e;
};


class D : public B
{
public:
	D() : d(3)
	{
		cout << "Constructor D: d=" << d << endl;
	}
	~D()
	{
		cout << "Destructor D" << endl;
	}
	int d;
	static B b; // zmienna statyczna typu klasy B?
	E e;
};


B D::b; // co tutaj się dzieje właściwie?

int main()
{
	cout << "Main start" << endl;
	D d1;

	cout << "New allocation" << endl;
	C *c1 = new C[2];
	delete[] c1;
	return 0;
}

nie mogę zrozumieć linijek z komentarzami, kiedy aktywuje się konstruktor zmiennej statycznej? co musi zostać zdefiniowane?

1

B D::b; // co tutaj się dzieje właściwie?

Inicjalizacja objektu statycznego klasy B (static B b;) konstruktorem domyślnym klasy B.
Inicjalizacja obiektu statycznego jak w powyższym przykładzie musi być poza ciałem klasy.

Jeszcze dorzucę: https://stackoverflow.com/questions/10499582/c-initialization-of-static-object-in-class-declaration

0

@YooSy: w sam punkt, dziękuję bardzo.

1 użytkowników online, w tym zalogowanych: 0, gości: 1