A
Alex Vinokur
Class Bar in a program below contains two static members: v1 and v2 of
class Foo.
v1 is initialized before creating an instance of Bar, v2 v1 is
initialized after that instance.
No Foo-constuctor for v2 was called before line
"Bar b".
However, (temporary ?) instance for v2 was created in Bar::Bar(). How
was it created: without calling constructor?
------- foo.cpp -------
#include <iostream>
#include <vector>
using namespace std;
struct Foo : public vector<int>
{
Foo() { cout << "Foo::Ctor-0" << endl;}
Foo(size_t n) : vector<int> (n) { cout << "Foo::Ctor-1: n = " << n <<
endl;;}
};
struct Bar
{
static Foo v1;
static Foo v2;
static void Show(const char* const msg);
Bar();
};
Bar::Bar()
{
Show ("[Bar()-A]");
v1.push_back(0);
v2.push_back(0);
Show ("[Bar()-B]");
}
void Bar::Show(const char* const msg)
{
cout << msg << ": v1.size()" << v1.size() << ", v2.size() = " <<
v2.size() << endl;
}
Foo Bar::v1(100);
Bar b;
Foo Bar::v2(200);
int main()
{
Bar::Show ("[main] ");
return 0;
}
-----------------------
------- Run -------
Foo::Ctor-1: n = 100
[Bar()-A]: v1.size()100, v2.size() = 0
[Bar()-B]: v1.size()101, v2.size() = 1
Foo::Ctor-1: n = 200
[main] : v1.size()101, v2.size() = 200
-------------------
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
class Foo.
v1 is initialized before creating an instance of Bar, v2 v1 is
initialized after that instance.
No Foo-constuctor for v2 was called before line
"Bar b".
However, (temporary ?) instance for v2 was created in Bar::Bar(). How
was it created: without calling constructor?
------- foo.cpp -------
#include <iostream>
#include <vector>
using namespace std;
struct Foo : public vector<int>
{
Foo() { cout << "Foo::Ctor-0" << endl;}
Foo(size_t n) : vector<int> (n) { cout << "Foo::Ctor-1: n = " << n <<
endl;;}
};
struct Bar
{
static Foo v1;
static Foo v2;
static void Show(const char* const msg);
Bar();
};
Bar::Bar()
{
Show ("[Bar()-A]");
v1.push_back(0);
v2.push_back(0);
Show ("[Bar()-B]");
}
void Bar::Show(const char* const msg)
{
cout << msg << ": v1.size()" << v1.size() << ", v2.size() = " <<
v2.size() << endl;
}
Foo Bar::v1(100);
Bar b;
Foo Bar::v2(200);
int main()
{
Bar::Show ("[main] ");
return 0;
}
-----------------------
------- Run -------
Foo::Ctor-1: n = 100
[Bar()-A]: v1.size()100, v2.size() = 0
[Bar()-B]: v1.size()101, v2.size() = 1
Foo::Ctor-1: n = 200
[main] : v1.size()101, v2.size() = 200
-------------------
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn