• c++ 拷贝


    对应c++拷贝方式,我倒是较熟悉。但是对应指针的拷贝,我有些迷糊,所有做了一个简单的程序测试一下。

     1 //object.h
    2
    3 #ifndef OBJECT_H_
    4 #define OBJECT_H_
    5
    6 #include <iostream>
    7
    8 class object{
    9 public:
    10 object(){
    11 std::cout << "object()" << std::endl;
    12 }
    13
    14 object(const object&){
    15 std::cout << "object(const object&)" << std::endl;
    16 }
    17
    18 ~object(){
    19 std::cout << "~object()" << std::endl;
    20 }
    21 };
    22
    23 #endif /* OBJECT_H_ */


    main.cpp

     1 #include <iostream>
    2 #include "object.h"
    3
    4 int main(int argc, char **argv) {
    5 object obj1;
    6 object obj2(obj1);
    7 object obj3 = obj2;
    8
    9 std::cout << "---------------------" << std::endl;
    10
    11 object *pObj1 = new object();
    12 object *pObj2(pObj1);
    13
    14 std::cout << "pObj1=" << pObj1 << std::endl;
    15 std::cout << "pObj2=" << pObj2 << std::endl;
    16
    17 std::cout << "---------------------" << std::endl;
    18
    19 return 0;
    20 }


    输出结果:

    经过测试我们可以发现:

     object *pObj1 = new object();
     object *pObj2(pObj1);

    这只是一种简单的值(地址)的拷贝。现在,pObj1和pObj2指向同一个对象。

  • 相关阅读:
    HDU 1285
    HDU 1251
    HDU 1166
    UVA 10003
    HDU 5968
    CodeForces 371C
    POJ 2456
    POJ 2250
    oracle的start with connect by prior
    Hibernate session中的createCriteria方法
  • 原文地址:https://www.cnblogs.com/harrysun/p/2306766.html
Copyright © 2020-2023  润新知