C++ 代码编写注意

来源:互联网 发布:aws centos root密码 编辑:程序博客网 时间:2024/05/16 07:03

1.指针的赋值与初始化

  #pragma once

   class Temp

   {

     public:

        Temp();

        virtual ~Temp();

     public:

         Temp2* ptemp;

    }

     class Temp2

    {}

  

    类的实现中

    Temp::Temp()

    :ptemp(null)   //对指针初始化

    {

         ptemp = new Temp2() //才是对指针赋值

     }

 

     Temp::~Temp()

    {

         if(ptemp != null)     //释放

        {

               delete ptemp;

               ptemp = null;

         }

    }

 

2.   在c++中,C c1;                                                     C c1; C c2;

                                                    与

                     C c2 = c1;                                                  c1 = c2  是不一样的

 

     如果重载了运算符,希望进行深拷贝的话,则需要按第二种写法才能正确的进行深拷贝