zoukankan      html  css  js  c++  java
  • Copy Control 复制控制 (复制构造函数 copy constructor,析构函数 destructor, 赋值操作符 operator=

    #include <iostream>
    
    using namespace std;
    
    class Point{
    public:
        Point(int cx,int cy):x(cx),y(cy){
            pData=new int;
            *pData = 0;
        }
        Point(const Point& pt)  //copy constructor
        {
            x=pt.getX();
            this->y=pt.getY();
            pData=new int;
            *pData=pt.getData();
        }
        void to_str(){
            cout<<"x="<<x<<",y="<<y<<",*pData="<<*pData<<endl;
        }
        void setX(int x){
            this->x=x;
        }
        void setY(int y)
        {
            this->y=y;
        }
        void setData(int data)
        {
            *pData=data;
        }
        int getX() const
        {
            return x;
        }
        int  getY() const
        {
            return y;
        }
        int  getData() const
        {
            return *pData;
        }
        virtual ~ Point()
        {
            delete pData;
        }
    private:
        int x,y,*pData;
    
    };
    
    int main()
    {
        Point pt(1,2);
        pt.setData(123);
        pt.to_str();
    
        cout<<"Hello中国"<<endl;
        Point pt2(pt);
        pt2.setX(11);
        pt2.setY(22);
        pt2.setData(112233);
    
        pt.to_str();
        pt2.to_str();
        return 0;
    }
    

      

    关于数据成员是否设置get,set方法,参考 Google Cpp Style Guide

    Access Control

    link

    Make data members private, and provide access to them through accessor functions as needed (for technical reasons, we allow data members of a test fixture class to be protected when using Google Test). Typically a variable would be called foo_ and the accessor function foo(). You may also want a mutator functionset_foo(). Exception: static const data members (typically called kFoo) need not be private.

    The definitions of accessors are usually inlined in the header file.

    See also Inheritance and Function Names.

    http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml?showone=Access_Control#Access_Control

  • 相关阅读:
    单词统计
    意见汇总
    项目评审结果
    注册表键值
    C++ 创建快捷方式
    XPosed 示例
    直播流程
    C++隐藏任务栏图标
    C++ 屏幕录制
    DUILIB UI创建过程
  • 原文地址:https://www.cnblogs.com/wucg/p/2438645.html
Copyright © 2011-2022 走看看