段错误。。。拷贝构造函数未赋值导致得异常

#include 
#include 
#include 
#include 
#include 
class B{
    public:
        inline void set( int b){
            m_b = b;
	    }
        inline int get(){
            return m_b;
	    }
    private:
        int m_b;
};

class A{
    public:
        A(){
            m_b = new B;
            std::cout << "A 构造函数" << std::endl;
        }
        A(const A& a){
            this->m_a = a.m_a;//事故点:另一同事写的,此函数起初未赋值,导致找了一天两夜。。。
            this->m_b = a.m_b;//后经找到原同事才点了下。。。
            std::cout << "A 拷贝构造函数" << std::endl;
        }
        inline void set( int a){
            m_a = a;
	    }
        inline int get(){
            return m_a;
	    }
        inline B * getB(){
            return m_b;
	    }
    private:
        int m_a;
        B * m_b;
};

template
class Sequence{
    public:
        inline void append( const T& item ){
		    m_data.push_back(item);
	    }
        int size(){
            return m_data.size();
        }
        T& getItem(int i){
            return m_data[i];
        }
    private:
        std::vector m_data;
};

int main(){
    Sequence cax;
    A *a = new A;
    printf("A:%x\n",a);
    a->set(125);
    a->getB()->set(123);
    printf("B:%x\n",a->getB());

    cax.append(*a);
    for(int i = 0;i < cax.size();i++){
        A *p = dynamic_cast(&cax.getItem(i));
        printf("A:%x,B:%x\n",p,p->getB());
		std::cout << "a:" << p->get() << "b:" << p->getB()->get()<< std::endl;
    }

    return 0;
}

段错误。。。拷贝构造函数未操作导致得异常。

如果将事故点去掉,会

段错误。。。拷贝构造函数未赋值导致得异常_第1张图片

 如果加上事故点,才能正常运行。

段错误。。。拷贝构造函数未赋值导致得异常_第2张图片 

基础要时常回顾。。。

你可能感兴趣的:(c++,蓝桥杯,c++,拓扑学)