#include <iostream> #include <vector> #include <map> using namespace std; //1.相当于typedef的作用 using my_vec=vector<int>; void test_using1() { my_vec vec={1,2,3,4,5,6}; for(auto i:vec) cout<<i<<" "; cout<<endl; } //typedef不能重命名模板,只有using可以,用typedef会得到error: a typedef cannot be a template的错误信息。 template <typename T> using my_str=map<T,string>; void test_using2() { my_str<int> str; pair<map<int,string>::iterator,bool> p=str.insert(make_pair(666,"aaa"));//piar<map<int,string>::iterator,bool>可改为auto cout<<"map.first->first:"<<p.first->first<<" map.first->second:"<<p.first->second <<" map.second:"<<p.second<<endl; } /* *2.using父类方法,主要是用来实现可以在子类实例中调用到父类的重载版本 * 访问父类成员 */ class Base { public: Base(int a):num(a){} void show() { cout<<"this is base."<<endl; } void show(int a) { cout<<"this is base with int:"<<a<<endl; } protected: int num; }; class Ship:public Base { public: Ship(int a,int b):Base(a),num1(b){} using Base::show;//using只能指定一个名字不能带形参,且基类的该函数不能有私有版本 using Base::num;//num在Base中位protected,径private继承后,成为private, Ship无法使用,用using便可使用 void shows(int a) { cout<<"this is Ship with int:"<<a<<endl; } private: int num1; }; int main() { test_using1(); test_using2(); Base b(6); b.show(); Ship s(6,9); s.show(555); s.shows(666); return 0; }
c++中using的使用
猜你喜欢
转载自www.cnblogs.com/tianzeng/p/9775041.html
今日推荐
周排行