我最近在一个类中看到了这个构造函数:
public MyClass(){ }
Run Code Online (Sandbox Code Playgroud)
没有其他建设者.
是否有一个原因?Java会自动创建一个默认构造函数,那么为什么要显式声明一个呢?或者这被认为是一种好的做法,就像对单语句if语句使用大括号一样 - 如果稍后添加其他构造函数而你忘记了你没有默认值......?
以下是否符合C++ 11标准(= default在类的定义之外)?
// In header file
class Test
{
public:
Test();
~Test();
};
// In cpp file
Test::Test() = default;
Test::~Test() = default;
Run Code Online (Sandbox Code Playgroud) 我已经看了一些关于这个问题的其他问题,但我不明白为什么在我的情况下甚至应该调用默认构造函数.我可以提供一个默认的构造函数,但我想了解它为什么这样做以及它会影响什么.
error C2512: 'CubeGeometry' : no appropriate default constructor available
Run Code Online (Sandbox Code Playgroud)
我有一个名为ProxyPiece的类,其成员变量为CubeGeometry.构造函数应该接受CubeGeometry并将其分配给成员变量.这是标题:
#pragma once
#include "CubeGeometry.h"
using namespace std;
class ProxyPiece
{
public:
ProxyPiece(CubeGeometry& c);
virtual ~ProxyPiece(void);
private:
CubeGeometry cube;
};
Run Code Online (Sandbox Code Playgroud)
和来源:
#include "StdAfx.h"
#include "ProxyPiece.h"
ProxyPiece::ProxyPiece(CubeGeometry& c)
{
cube=c;
}
ProxyPiece::~ProxyPiece(void)
{
}
Run Code Online (Sandbox Code Playgroud)
立方体几何体的标题如下所示.使用默认构造函数对我没有意义.我还需要吗?:
#pragma once
#include "Vector.h"
#include "Segment.h"
#include <vector>
using namespace std;
class CubeGeometry
{
public:
CubeGeometry(Vector3 c, float l);
virtual ~CubeGeometry(void);
Segment* getSegments(){
return segments;
}
Vector3* getCorners(){
return corners;
}
float getLength(){
return length;
} …Run Code Online (Sandbox Code Playgroud) 该锵文档整齐地解释说,
如果类或结构没有用户定义的默认构造函数,C++不允许您默认构造它的const实例([dcl.init],p9)
基本原理是如果const对象未正确初始化,则以后不能更改.以下代码仅具有用户声明的默认构造函数Test,但其所有成员都具有类内初始值设定项,
#include<iostream>
class Test
{
public:
Test() = default;
void print() const { std::cout << i << "\n"; }
private:
int i = 42; // will propagate to the default constructor!
};
int main()
{
Test const t; // <-- Clang chokes on the const keyword, g++ does not
t.print(); // prints 42
}
Run Code Online (Sandbox Code Playgroud)
所以用户提供默认构造函数的基本原理对我来说似乎是多余的.事实上,g ++ 4.8.1确实可以毫无问题地编译它(在线示例),尽管Clang <= 3.2没有.
问题:为什么完整的类内initalizers +用户声明的默认构造函数的组合不足以默认构造一个const对象?是否有针对C++ 14标准的修复程序?
更新:任何人都可以尝试使用Clang 3.3/3.4,看看与Clang …
int并object有一个无参数的构造函数.为什么不string呢?
打印b.k时为什么打印会发出警告a.k?我使用VS2013
//warning C4700: uninitialized local variable 'b' used
#include<iostream>
using namespace std;
struct A {
A() {};
int k;
};
struct B {
B() = default;
int k;
};
int main() {
A a;
cout << a.k << endl;
B b;
cout << b.k << endl; // this gives a warning, uninitialized local variable
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当我编译#if 0和#if 1下面代码的版本时,如何解释差异:
#include <cstdlib>
struct A
{
explicit A() = default; // explicitly defaulted or deleted constructors are allowed for aggregates (since C++11)
#if 1
private :
#endif
int i;
};
int
main()
{
A a = {};
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
#if 0所有的罚款,编译成功.#if 1编译失败,错误信息:
错误:选择的构造函数在复制初始化中是显式的
表达式A a = {};取决于是否A为aggreagate有什么区别?
这有什么区别:
TestClass t;
Run Code Online (Sandbox Code Playgroud)
还有这个:
TestClass t = TestClass();
Run Code Online (Sandbox Code Playgroud)
我希望第二个可能会调用构造函数两次然后调用operator =,而是调用构造函数一次,就像第一次一样.
以下代码使用GCC 8.2编译,但不与Clang 6.0.1编译:
// A struct named Foo.
struct Foo
{
// Data member of type 'int'.
int val;
// Default constructor (constexpr).
constexpr Foo() noexcept : val(0) {}
};
// A struct named Bar.
struct Bar : Foo
{
// Make use of the constructors declared in Foo.
using Foo::Foo;
// A constructor taking an object of type Foo.
// COMMENTING THIS CONSTRUCTOR SOLVE THE COMPILATION ISSUE.
constexpr Bar(Foo const obj) noexcept : Foo(obj) {}
};
// A struct …Run Code Online (Sandbox Code Playgroud)