gpt4 book ai didi

c++ - 基类构造函数的 using 声明

转载 作者:太空狗 更新时间:2023-10-29 21:09:48 24 4
gpt4 key购买 nike

乍一看,using 用于导入特定函数(例如using std::cout 到作用域)。但是这个using实际上是将基类的所有构造函数导入到派生类中。这个 using 声明的幕后究竟是什么?

template< typename T >
class Vec : public std::vector< T >
{
public:
using std::vector<T>::vector; // ?

//...
};

最佳答案

As it's public inherited, supposedly all the base class constructors should have been available already (ie why need using)?

No,默认不继承基类的构造函数。可以在以下讨论中找到详细解释:


What's actually behind the scene of this using declaration?

来自 cppreference.com 使用可以

  1. Using-declarations can be used to introduce namespace members into other namespaces and block scopes, or to introduce base class members into derived class definitions.
  2. [...] ( specific...)

同时 inheritance :

If the using-declaration refers to a constructor of a direct base of the class being defined (e.g. using Base::Base;), all constructors of that base (ignoring member access) are made visible to overload resolution when initializing the derived class.

关于c++ - 基类构造函数的 using 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57105400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com