C++笔记-using的进一步用法
生活随笔
收集整理的這篇文章主要介紹了
C++笔记-using的进一步用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
基本概念
代碼與實例
?
基本概念
最近想學習點C++ 11的特性,置于為什么想學這個,你懂的!
在此記錄下,今天關于using的進一步用法,免得以后只會用using namespace xxx
這里關于using有3個功能:
? ? ? ? ? 1. 命名空間;
? ? ? ? ? 2. 別名;
? ? ? ? ? 3. 重寫設定訪問權限。
?
代碼與實例
程序運行截圖如下:
源碼如下:
head.h
#ifndef HEAD_H #define HEAD_H#include <iostream> using namespace std;class Base{public:int value1;int value2; };class Child : private Base{public://重寫設定訪問權限using Base::value1; };#endifmain.cpp
#include "head.h"int getSum(const int &a, const int &b){return a + b; }//別名 using HelloWorld = int; typedef int HowOldAreYou;using SUMFUNCTION = int(*)(const int &a, const int &b); typedef int (*ADD)(const int &a, const int &b);int main(){HelloWorld value1 = 99;HowOldAreYou value2 = 10086;cout << "The value1 is : " << value1 << endl;cout << "The value2 is : " << value2 << endl;Child child;child.value1 = 999;ADD p = getSum;cout << "The sum Value is : " << p(100, 200) << endl;SUMFUNCTION sumFunction = getSum;cout << "The sum value is : " << sumFunction(2, 10000) << endl;getchar();return 0; }using的別名功能個人覺得和typedef一模一樣,為什么這么說呢!
打開vs,鼠標移動上去就知道了!
總結
以上是生活随笔為你收集整理的C++笔记-using的进一步用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-concurrent集合及
- 下一篇: Arduino笔记-ESP8266模块实