C++ 中 using 关键字的用法

linbojue
• 阅读 0

C++ 中的 using 用法有很多种,我们下面剖析下常见几种场景:

using 关键字给类型增加别名 using 用于继承中的同名函数 using 关键字用于构造函数

  1. using 关键字给类型增加别名 typedef int my_int1; using my_int2 = int; void test04() { my_int1 a = 100; my_int2 b = 200; } 对于普通类型 using 和 typedef 用法很类似,using 主要能力在于模板泛型中定义类型别名,如下代码所示:

//typedef std::vector my_type; // 如果定义带有模板参数的类型, typedef 无能为力 //template typedef std::vector my_type; // 报错 template using my_type = std::vector ; // 正确 所以, using 更加能够适应 C++ 泛型编程的场景。

  1. using 用于继承中的同名函数 当子类和父类的函数重名之后,会自动隐藏父类的同名函数。此时,如果要用父类的函数,就必须增加父类类作用域来访问。使用 using 可以避免隐藏父类的同名函数。

#include using namespace std; class Base1 { public: void show(int) { cout << "Base1::show(int)" << endl; } void show(int, int) {} }; class Derived1 : Base1 { public: // 父类的函数禁止在子类中隐藏 using Base1::show; void show() {} // 使用 using 禁止隐藏父类同名函数 // 假设子类和父类的函数的版本一样(参数个数、参数类型、函数名),此时还是会隐藏父类函数。 void show(int) { cout << "Derived1::show(int)" << endl; } }; void test05() { Derived1 d; // d.show(); d.show(10); // d.show(10, 20); } 3. using 关键字用于构造函数 using 可以继承父类的构造函数,对于子类的成员可以使用就地初始化。 注意:父类的构造函数,如果没有默认的话,不会自动继承。

class Base2 { public: Base2(int a) { m_a = a; m_b = 0; } Base2(int a = 100, int b= 200) { cout << "Base2::Base2(int, int)" << endl; m_a = a; m_b = b; } public: int m_a; int m_b; }; class Derived2 : Base2 { public: using Base2::Base2; /* Derived2(int a, int b) : Base2(a, b) { cout << "Derived2::Derived2(int, int)" << endl; } */

/*
Derived2(int a) : Base2(a) {}
Derived2(int a, int b) : Base2(a, b) {}
*/

public: int m_c{}; int m_d{}; }; void test06() { // 1. 由于C++编译期要求:如果父类没有默认构造, // 需要初始化列表指定构造函数。所以,子类必须增加构造函数。 // 2. 假设:能够把父类的构造函数继承下来,这样的话, // 子类就不需要额外去写一些没有意义的构造函数。 // Derived2 d1(10); Derived2 d2(10, 20); }

以下列子: https://infogram.com/untitled-1h0n25opq07el4p https://infogram.com/untitled-1hnq41op8yvop23 https://infogram.com/untitled-1hnp27eqd08ky4g https://infogram.com/untitled-1h1749wqz058l2z https://infogram.com/untitled-1h1749wqz058l2z https://infogram.com/9862pdf-1h7v4pd0lvoq84k https://infogram.com/9862pdf-1h984wv15w9kd2p https://infogram.com/9862pdf-1h9j6q759qv7v4g https://infogram.com/untitled-1hnq41op8jk9p23 https://infogram.com/9862pdf-1hxj48mqgj5152v

点赞
收藏
评论区
推荐文章
小万哥 小万哥
2年前
C++中static关键字的作用
static是什么在最开始C中引入了static关键字可以用于修饰变量和函数,后来由于C引入了class的概念,现在static可以修饰的对象分为以下5种:成员变量,成员函数,普通函数,局部变量,全局变量static的作用修饰成员变量static修饰成
隔壁老王 隔壁老王
4年前
我的C语言基础
C语言32个关键字auto声明自动变量short声明短整型变量或函数int声明整型变量或函数long声明长整型变量或函数float声明浮点型变量或函数double声明双精度变量或函数char声明字符型变量或函数struct声明结构体变量或函数union声明共用数据类型enum声明枚举类型typedef用以给数据类型取别名co
Stella981 Stella981
4年前
ASP.NET MVC 5 之 理解强类型View&ViewModel
viewbag和viewbag都有问题,强类型view才是王道。使用强类型View1\. @using(http://my.oschina.net/using)WebApplication.Mdels//引用命名空间,不能将View设置为多个Model使用的强类型    @model(http://my.o
Stella981 Stella981
4年前
Consider using the `
这是因为权限问题,被拒绝。在命令行添加user例如:pipinstallopencvcontribpythonuseropencvcontribpython:该face模块实际上并不是opencv图书馆的一部分。更确切地说,face是部分的的opencvcontrib库。从自述文件:此存储库\opencv
可莉 可莉
4年前
19、C语言 —— typedef
1、关键字typedef可以为各种数据类型定义一个新的名字(别名)typedef int Integer;    // 为int起个别名叫IntegerInteger i  0;    // 相当于 int i  0;2、给指针定义一个别名// 之前我们是这样定义一个指针的char 
Wesley13 Wesley13
4年前
C#压缩解压缩文件(zip格式)
using System;using System.Collections.Generic;using System.IO;using ICSharpCode.SharpZipLib.Zip;namespace TestConsole{    internal class Prog
Stella981 Stella981
4年前
C#之json字符串转xml字符串
留爪参考using System.Xml; //using System.Text; //using System.Runtime.Serialization.Json; //JsonReaderWriterFactory//以下method引用以上using    /// <summary
Stella981 Stella981
4年前
C++关键字之using的的用法总结
Cusing用法总结1)配合命名空间,对命名空间权限进行管理usingnamespacestd;//释放整个命名空间到当前作用域usingstd::cout;//释放某个变量到当前作用域2)类型重命名作用等同typedef,但是逻辑上更直观。include<iostream
Wesley13 Wesley13
4年前
Unity中使用多构造函数
如果要实例化的类只有一个构造函数,则使用方法很简单使用方法如下:1234567using(IUnityContainercontainernew UnityContainer()){UnityConfigurationSectionsection(UnityConfigura
Stella981 Stella981
4年前
C# using 别名
场景重现当using的多个库出现类名重复的情况时...解决办法1.使用类的_完全限定名称_,例如://不需要using,避免using名称重复导致的异常//使用类的完全限定名称,俗称全名.System.Timers.Timertimernew