我有一个类Bar
引用了它的一个成员(Bar::foo
)的内部:
#include<vector>
#include<algorithm>
struct Foo{
int x, y;
};
struct Bar{
Foo foo;
int &x, &y;
Bar(): x(foo.x), y(foo.y){}
// copy constructor and assignment operator
Bar(const Bar& other): foo(other.foo), x(foo.x), y(foo.y){}
Bar& operator=(const Bar& other){ foo=other.foo; return *this; }
};
int main(void){
std::vector<Bar> a, b;
Bar p; p.x=0; p.y=0;
a.push_back(p);
std::copy(a.begin(),a.end(),b.begin());
}
使用(没有任何特殊选项)进行编译g++
,我在赋值运算符中遇到崩溃。为什么?
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400b29 in Bar::operator= (this=0x0, other=...) at ref2.cpp:14
14 Bar& operator=(const Bar& other){ foo=other.foo; return *this; }
(gdb) bt
#0 0x0000000000400b29 in Bar::operator= (this=0x0, other=...) at ref2.cpp:14
#1 0x00000000004016a0 in std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m<Bar*, Bar*> (
__first=0x405010, __last=0x405030, __result=0x0) at /usr/include/c++/4.6/bits/stl_algobase.h:329
#2 0x000000000040148d in std::__copy_move_a<false, Bar*, Bar*> (__first=0x405010, __last=0x405030, __result=0x0)
at /usr/include/c++/4.6/bits/stl_algobase.h:384
#3 0x0000000000401157 in std::__copy_move_a2<false, __gnu_cxx::__normal_iterator<Bar*, std::vector<Bar, std::allocator<Bar> > >, __gnu_cxx::__normal_iterator<Bar*, std::vector<Bar, std::allocator<Bar> > > > (__first=..., __last=...,
__result=...) at /usr/include/c++/4.6/bits/stl_algobase.h:422
#4 0x0000000000400cc0 in std::copy<__gnu_cxx::__normal_iterator<Bar*, std::vector<Bar, std::allocator<Bar> > >, __gnu_cxx::__normal_iterator<Bar*, std::vector<Bar, std::allocator<Bar> > > > (__first=..., __last=..., __result=...)
at /usr/include/c++/4.6/bits/stl_algobase.h:454
#5 0x00000000004009f4 in main () at ref2.cpp:21