我不明白为什么你不能编译一个既有成员(不是默认可构造)又带有大括号或者相同的初始化器和继承的构造函数的类.g ++说:
test.cpp:22:15:错误:使用已删除的函数'Derived :: Derived(float)'
派生d(1.2f);test.cpp:16:13:注意:'Derived :: Derived(float)'被隐式删除,
因为默认定义不正确:
使用Base :: Base;test.cpp:16:13:错误:没有匹配函数调用'NoDefCTor :: NoDefCTor()'
test.cpp:5:1:注意:候选:
NoDefCTor :: NoDefCTor(int)NoDefCTor(int){}
无法编译的代码(在g ++ 5.1下):
struct NoDefCTor
{
NoDefCTor(int) {}
};
struct Base
{
Base(float) {}
};
struct Derived : Base
{
using Base::Base;
NoDefCTor n2{ 4 };
};
int main()
{
Derived d(1.2f);
}
Run Code Online (Sandbox Code Playgroud)
编译的代码,但从不使用 NoDefCTor默认构造函数(尽管显然需要它!):
struct NoDefCTor
{
NoDefCTor(int) {}
NoDefCTor() = default;
};
struct Base
{
Base(float) {}
};
struct Derived : Base
{
using Base::Base;
NoDefCTor …Run Code Online (Sandbox Code Playgroud) [...]命名构造函数的using声明不会创建同义词; 相反,如果在用于构造相应基类的对象时可以访问附加构造函数,则可以访问这些附加构造函数,并忽略using-declaration的可访问性.[...]
因此,以下代码无法编译:
class B { protected: B(int) { } };
class D: B { using B::B; };
int main () { D d{0}; }
Run Code Online (Sandbox Code Playgroud)
它返回的错误与所有主要编译器大致相同:
在这里宣布受保护
另一方面,以下代码编译:
class B { protected: B() { } };
class D: B { using B::B; };
int main () { D d{}; }
Run Code Online (Sandbox Code Playgroud)
它是否应该因为导致上一个示例中的错误的相同原因而无法编译?
它允许编译什么?
c++ default-constructor language-lawyer c++11 inherited-constructors
我知道默认构造函数不是继承的,如n3337中所述.
那里有一个例子:
struct B2 {
B2(int = 13, int = 42);
};
struct D2 : B2 {
using B2::B2;
};
Run Code Online (Sandbox Code Playgroud)
有很好的解释:
D2for 的继承构造函数的候选集B2是Run Code Online (Sandbox Code Playgroud)... —B2(int = 13, int = 42) —B2(int = 13) —B2()
最重要的是:
存在的构造函数集
D2是
—D2()隐式声明的默认构造函数,不是继承的
对我来说,这个例子没有显示出差异,从某种意义上说,即使这个构造函数是继承的 - 它的行为与隐式声明的默认构造函数没有区别.
我需要一个示例来说明可以轻松理解的方式的差异,比如说,熟悉C++ 03但想要学习C++ 11的观众.
[更新]
所有答案(包括我自己的答案)都是善良的" 如果继承了默认的c-tor,那么示例将编译/不编译 ".
我更喜欢答案,其中结果(可观察行为)与其他情况不同.
c++ inheritance default-constructor c++11 inherited-constructors
struct B {
B(int) {}
B(B const&) {}
};
struct D: B {
using B::B;
};
int main(void) {
B b(5);
D d(b); // error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
c++14 在 12.9 [class.inhctor]/p3 中明确从继承的构造函数中排除复制/移动构造函数。
对于候选继承构造函数集中的每个非模板构造函数(除了没有参数的构造函数或具有单个参数的复制/移动构造函数之外),构造函数将隐式声明为具有相同的构造函数特征,除非存在用户声明的构造函数完整类中出现 using 声明的相同签名,或者构造函数将是该类的默认构造函数、复制构造函数或移动构造函数。
但我在c++17中找不到任何详细的描述。clang/gcc 显示基类的复制/移动构造函数不是继承的。有人可以提供标准中的解释吗?谢谢。
这是类foo:
template <typename T>
struct foo
{
foo()
{
t = nullptr;
}
foo(T* p, bool flag)
{
t = p;
}
private:
T* t;
};
Run Code Online (Sandbox Code Playgroud)
这是班级栏:
template <typename T>
struct bar: public foo<T>
{
using foo<T>::foo<T>;
};
Run Code Online (Sandbox Code Playgroud)
它是继承构造函数的正确语法吗?如果我使用"使用foo :: foo;" 那么Visual C++ 2010的编译器就死了.那么基本上如何从VC++ 2010中的模板类继承构造函数呢?
c++ templates visual-studio-2010 c++11 inherited-constructors
对于以下程序:
#include <iostream>
struct Foo
{
Foo() { std::cout << "Foo()\n"; }
Foo(const Foo&) { std::cout << "Foo(const Foo&)\n"; }
~Foo() { std::cout << "~Foo()\n"; }
};
struct A
{
A(Foo) {}
};
struct B : A
{
using A::A;
};
int main()
{
Foo f;
B b(f);
}
Run Code Online (Sandbox Code Playgroud)
海湾合作委员会给出:
$ g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
Foo()
Foo(const Foo&)
~Foo()
~Foo()
Run Code Online (Sandbox Code Playgroud)
VS 2017(也在 C++17 模式下)给出:
Foo()
Foo(const Foo&)
Foo(const Foo&)
~Foo()
~Foo()
~Foo()
Run Code Online (Sandbox Code Playgroud)
谁是对的,为什么?
(我们也不要忘记 VS …
我希望子类使用其父类的构造函数.但似乎我总是需要在子类中再次定义它们才能使它工作,如下所示:
public SubClass(int x, int y) : base (x, y) {
//no code here
}
Run Code Online (Sandbox Code Playgroud)
所以我想知道我是不是在父类中正确地声明了构造函数,或者根本没有直接的构造函数继承?
下面的代码
#include <vector>
#include <string>
template<typename T>
struct V : public std::vector<T>
{
using Impl = std::vector<T>;
using typename Impl::vector; // the constructors
};
int main()
{
std::string empty;
V<std::string> meow{42UL, empty};
}
Run Code Online (Sandbox Code Playgroud)
由 GCC 8.2 编译良好(调用size_t, string构造函数)。然而, clang up to 14 拒绝它
<source>:14:20: error: no matching constructor for initialization of 'V<std::string>' (aka 'V<basic_string<char>>')
V<std::string> meow{42UL, empty};
^ ~~~~~~~~~~~~~
<source>:5:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided
struct V …Run Code Online (Sandbox Code Playgroud)