这是一个MWE,当使用编译时g++ -std=c++11
,会产生分段错误:
#include <iostream>
#include <random>
class Rand{
public:
Rand(double const& min_inclusive, double const& max_exclusive):
mt_(std::random_device()()),
dist_(min_inclusive,max_exclusive){}
~Rand(){}
double get() { return dist_(mt_); }
private:
std::mt19937_64 mt_;
std::uniform_real_distribution<double> dist_;
};
class Base {
public:
Base():rnd(0.0,1.0){ std::cout<<"Base created "<<&rnd<<" "<<rnd.get()<<std::endl; }
virtual ~Base(){};
Rand rnd;
};
class Child: public Base{
public:
Child():var(1.0){ std::cout<<"Child created"<<std::endl; }
double var;
};
class Other {
public:
Other(Base* b):b(b){ std::cout<<"Other created "<<&(b[0].rnd)<<" and "<<&(b[1].rnd)<<"--->"<<b[0].rnd.get()<<" "<<b[1].rnd.get()<<std::endl; }
Base* b;
};
int main(){
unsigned int N(2);
std::cout<<"#############"<<std::endl;
Base* b(new Base[N]);
Other o1(b);
std::cout<<"#############"<<std::endl;
Child* c(new Child[N]);
Other o2(c);
std::cout<<"#############"<<std::endl;
delete[] b;
delete[] c;
}
典型的输出是:
#############
Base created 0x239c020 0.226514
Base created 0x239ca00 0.902337
Other created 0x239c020 and 0x239ca00--->0.864321 0.302185
#############
Base created 0x239d3f0 0.573563
Child created
Base created 0x239ddd8 0.422187
Child created
Other created 0x239d3f0 and 0x239ddd0--->0.909183 4.94066e-324
#############
在第一部分中,Base
给Other
引用的类是连贯的,并且代码可以正常运行。
但是,当给一个Child
类时Other
,第一个引用是相同的,但第二个引用则略有不同。这样做的直接后果是,b[1].rdn.get()
始终(非常接近)为“ 0”。另一个间接后果是代码以SegFault结尾...
此外,如果Child::var
不存在该程序,则该程序可以正常运行,所有引用均一致,并且没有SegFault。
我究竟做错了什么 ?Other
用的子项创造是不可能的Base
吗?如果是这样,看起来多态性就毁了……
根据一个不错的答案:
#include <iostream>
#include <random>
#include <vector>
#include <memory>
class Rand{
public:
Rand(double const& min_inclusive, double const& max_exclusive):
mt_(std::random_device()()),
dist_(min_inclusive,max_exclusive){}
~Rand(){}
double get() { return dist_(mt_); }
private:
std::mt19937_64 mt_;
std::uniform_real_distribution<double> dist_;
};
class Base {
public:
Base():rnd(0.0,1.0){ std::cout<<"Base created "<<&rnd<<" "<<rnd.get()<<std::endl; }
virtual ~Base(){};
Rand rnd;
};
class Child: public Base{
public:
Child():var(0.0){ std::cout<<"Child created"<<std::endl; }
double var;
};
template<typename Type>
class Other {
public:
Other(unsigned int N):b_(N){
std::cout<<"Other created "<<std::endl;
for(unsigned int i(0);i<b_.size();i++){
b_[i] = std::make_shared<Type>();
}
}
std::vector<std::shared_ptr<Base> > b_;
};
int main(){
unsigned int N(2);
std::cout<<"#############"<<std::endl;
Other<Base> o1(N);
std::cout<<"#############"<<std::endl;
Other<Child> o2(N);
std::cout<<"#############"<<std::endl;
}
如果您有个数组,Child
则不能将其视为的数组Base
。这意味着,例如当您调用时b[1].rnd.get()
,您将调用未定义的行为。
这是为什么?当您拥有Child* c
并将其Base*
转换Base
为时,转换的结果是指向的子对象的指针Child
。因此(Base*)c + 1
将指向该子对象之后的下一个字节,在您的情况下为的第一个字节var
。
当Child
没有数据成员时,(Base*)c + 1
指向下一个对象,因此一切正常。
为了实现多态行为,你应该创建数组的指针到Base
:
Base** b{new Base*[N]};
使用的任何后代初始化每个数组元素,Base
并享受多态性。
编辑:正如vsoftco在评论中合理指出的那样,最好不要使用原始指针和数组:
std::vector<std::shared_ptr<Base>> b {...}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句