Пример #1
0
int main(void)
{
    int *pi = new int(42);
    HasPtr *hpa = new HasPtr(pi, 100);    // 构造函数
    HasPtr *hpb = new HasPtr(*hpa);     // 拷贝构造函数
    HasPtr *hpc = new HasPtr(*hpb);     // 拷贝构造函数
    HasPtr hpd = *hpa;     // 拷贝构造函数

    cout << hpa->get_ptr_val() << " " << hpb->get_ptr_val() << endl;
    hpc->set_ptr_val(10000);
    cout << hpa->get_ptr_val() << " " << hpb->get_ptr_val() << endl;
    hpd.set_ptr_val(10);
    cout << hpa->get_ptr_val() << " " << hpb->get_ptr_val() << endl;
    delete hpa;
    delete hpb;
    delete hpc;
    cout << hpd.get_ptr_val() << endl;
    return 0;
}
Пример #2
0
int main(void) {  
    int *pi = new int(42);
    HasPtr *hpa = new HasPtr(pi, 100);  // 构造函数
    HasPtr *hpb = new HasPtr(*hpa);     // 拷贝构造函数
    HasPtr *hpc = new HasPtr(*hpb);     // 拷贝构造函数
    HasPtr hpd = *hpa;     // 拷贝构造函数

    int *pj = new int(314);
    hpc = new HasPtr(pj, 200);

    cout << hpa->get_ptr_val() << " " << hpb->get_ptr_val() << endl;  
    hpc->set_ptr_val(10000);  
    cout << hpa->get_ptr_val() << " " << hpc->get_ptr_val() << endl;  
    cout << hpa->get_ptr_val() << " " << hpb->get_ptr_val() << endl;  
    hpd.set_ptr_val(10);  
    cout << hpa->get_ptr_val() << " " << hpb->get_ptr_val() << endl;  
    delete hpa;  
    delete hpb;  
    delete hpc;  
    cout << hpd.get_ptr_val() << endl;  
    return 0;  
}