【发布时间】:2016-02-16 14:29:10
【问题描述】:
提前致谢。
我在一些实际项目中看到了这些代码。我只是简化了它们来表达我的问题。基类将 this 指针放入其构造函数中的 vector(vec) 中。使用这个技巧,我们可以利用动态绑定在外部(在 main func 中)调用派生类的方法。关键是将向量定义放在Bash.h中,声明放在main.cpp中。
我的问题是,输出不是我想要的。为什么 vec 在一个 Translation 单元(base.c,我们可以看到大小为 1)中初始化并使用,然后在其他 Translation 单元(main.c,我们可以看到大小变成了0),是不是又被初始化清零了?我想知道不同翻译单元的初始化顺序。好像 Derive::dInstance 是第一位的,vec 是第二位的,但是为什么呢?
Base.h
#ifndef BASE_H
#define BASE_h
#include<iostream>
#include<vector>
using namespace std;
class Base{
public:
Base();
virtual void speak();
};
#endif
Base.cpp
#include"Base.h"
vector<Base*> vec;
Base::Base() {
vec.push_back(this);
cout << "Base's constructor" << endl;
cout << "Base() vector size: " << vec.size() << endl;**
}
void Base::speak() {
cout << "I am Base" << endl;
}
导出.h
#ifndef DERIVE_H
#define DERIVE_h
#include<iostream>
#include"Base.h"
class Derive: public Base {
public:
Derive();
virtual void speak();
static Derive dInstance;
};
#endif
导出.cpp
#include "Derive.h"
// static member definition
Derive Derive::dInstance;
Derive::Derive() {
cout << "Derived's construtor" << endl;**
}
void Derive::speak() {
cout << "I am Derived" << endl;
}
main.cpp
#include<iostream>
#include"Base.h"
using namespace std;
extern vector<Base*> vec;
int main(int argc, char *argv[]) {
cout << "vector size: " << vec.size() << endl;
for(vector<Base*>::iterator iter = vec.begin(); iter != vec.begin(); iter++) {
(*iter)->speak();
}
}
输出:
Base的构造函数 Base() 向量大小:1 Derived 的构造函数 main() 向量大小:0【问题讨论】:
标签: c++ design-patterns initialization singleton translation-unit