• 面试总结


    1、左值和右值

    C++里定义:左值是即可出现在等号左边又可以出现在等号右边的变量(或者表达式);右值是只能出现在等号右边的变量(或表达式)。

    对于C和C++基础类型中:右值都是不可修改的,也不能用Const 来修饰;但在C++中自定义的类型来讲,右值可以被成员函数修改。

    2、关于智能指针auto_ptr,你了解多少?

    包含在头文件memory中。

    主要是为了解决“发生异常后导致内存泄漏”这个问题。

    (源码摘抄供大家看看):

    namespace std
    {
     template<class T>    //类模板
     class auto_ptr 
     {
     private:
      T* ap;      //一般指针,,,而不是数组指针
     public:
    
      // constructor & destructor ----------------------------------- (1)
      explicit auto_ptr (T* ptr = 0) throw() : ap(ptr){}
    
      ~auto_ptr() throw() 
      {
       delete ap;
      }
    
      
      // Copy & assignment --------------------------------------------(2)
      auto_ptr (auto_ptr& rhs) throw() :ap(rhs.release()) {}
      template<class Y>  
      auto_ptr (auto_ptr<Y>& rhs) throw() : ap(rhs.release()) { }
    
      auto_ptr& operator= (auto_ptr& rhs) throw() 
      {
       reset(rhs.release());
       return *this;
      }
      template<class Y>
      auto_ptr& operator= (auto_ptr<Y>& rhs) throw() 
      {
       reset(rhs.release());
       return *this;
      }
    
      // Dereference----------------------------------------------------(3)
      T& operator*() const throw() 
      {
       return *ap;
      }
      T* operator->() const throw() 
      {
       return ap;
      }
    
      // Helper functions------------------------------------------------(4)
      // value access
      T* get() const throw() 
      {
       return ap;
      }
    
      // release ownership
      T* release() throw()
      {
       T* tmp(ap);
       ap = 0;
       return tmp;
      }
    
      // reset value
      void reset (T* ptr=0) throw() 
      {
       if (ap != ptr) 
       {
        delete ap;
        ap = ptr;
       }
      }
    
      // Special conversions-----------------------------------------------(5)
      template<class Y>
      struct auto_ptr_ref
      {
       Y* yp;
       auto_ptr_ref (Y* rhs) : yp(rhs) {}
      };
    
      auto_ptr(auto_ptr_ref<T> rhs) throw() : ap(rhs.yp) { }
      auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() 
      {  
       reset(rhs.yp);
       return *this;
      }
      template<class Y>
      operator auto_ptr_ref<Y>() throw() 
      {
       return auto_ptr_ref<Y>(release());
      }
      template<class Y>
      operator auto_ptr<Y>() throw()
      {
       return auto_ptr<Y>(release());
      }
     };
    }
    

      

  • 相关阅读:
    PHP7中php.ini、php-fpm和www.conf的配置(转)
    Linux下查看nginx、mysql、php的安装路径和编译参数
    Skip List(跳跃表)原理详解与实现【转】
    offset宏的讲解【转】
    Linux内核中的常用宏container_of其实很简单【转】
    Linux内核同步原语之原子操作【转】
    创建与合并分支【转】
    Git远程操作详解【转】
    【项目管理】git和码云的使用【转】
    Git和Github简单教程【转】
  • 原文地址:https://www.cnblogs.com/westlife-11358/p/9336434.html
Copyright © 2020-2023  润新知