假设我有一个类型,我想将其默认构造函数设为私有.我写了以下内容:
class C {
C() = default;
};
int main() {
C c; // error: C::C() is private within this context (g++)
// error: calling a private constructor of class 'C' (clang++)
// error C2248: 'C::C' cannot access private member declared in class 'C' (MSVC)
auto c2 = C(); // error: as above
}
Run Code Online (Sandbox Code Playgroud)
大.
但是,构造函数结果证明不像我想象的那样私密:
class C {
C() = default;
};
int main() {
C c{}; // OK on all compilers
auto c2 = C{}; // OK on …Run Code Online (Sandbox Code Playgroud) c++ default-constructor language-lawyer aggregate-initialization c++11