zoukankan      html  css  js  c++  java
  • return与析构续

    #include <iostream>
    using namespace std;
    class Point{
    private:
        int x,y;
    public:
        Point(int a,int b=0){
            x=a;y=b;
            cout<<"normal"<<endl;
        }
        Point(const Point &p){
            x=2*p.x;
            y=2*p.y;
            cout<<"copy"<<endl;
        }
        void print(){
            cout<<x<<" "<<y<<endl;
        }
        ~Point(){
            cout<<"destructing"<<endl;
        }
    };
    void f1(Point p){
        p.print();
    }
    Point f3(Point p){
        return p;
    }
    Point f2(){
        Point p(10,30);
        return p;
    }
    int main(){
        Point p4(1,1);
        cout<<"p4"<<endl;
        p4.print();
        Point p2=f3(p4);
        p2.print();
        return 0;

    }

    结果:

    normal
    p4
    1 1
    copy
    copy
    destructing
    4 4
    destructing
    destructing

    若主函数为:

    int main(){
        Point p4(1,1),p2(1,1);
        cout<<"p4"<<endl;
        p4.print();
        p2=f3(p4);
        p2.print();
        return 0;

    }

    结果:

    normal
    normal
    p4
    1 1
    copy
    copy
    destructing
    destructing//原来的p2析构
    4 4
    destructing
    destructing

  • 相关阅读:
    EJB3.0
    EJB
    到底EJB是什么
    EJB到底是什么,真的那么神秘吗??
    ssh和ejb的区别
    struts2的一些小问题
    Struts2中ValueStack结构和总结
    Java 监听器
    浅谈cookie 和session 的区别
    理解Cookie和Session机制
  • 原文地址:https://www.cnblogs.com/wangjianupc/p/10587213.html
Copyright © 2011-2022 走看看