zoukankan      html  css  js  c++  java
  • 面试总结

    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());
      }
     };
    }
    

      

  • 相关阅读:
    如何让实现整站变灰色调 (一般用于悼念活动)
    .net 泛型集合排序,查找
    Windows 2003服务器远程桌面配置
    .net 读取文件数据
    .net 验证工具类
    MS-SQL 基本语法大全
    SQL NVARCHAR和VARCHAR限制
    SQL SERVER DATETIME应用
    SQL事务在存储过程的应用
    移除button点击时的黑边
  • 原文地址:https://www.cnblogs.com/westlife-11358/p/9336434.html
Copyright © 2011-2022 走看看