标签: default-constructor

当没有其他构造函数时,是否有理由显式编写默认构造函数?

我最近在一个类中看到了这个构造函数:

public MyClass(){ }
Run Code Online (Sandbox Code Playgroud)

没有其他建设者.

是否有一个原因?Java会自动创建一个默认构造函数,那么为什么要显式声明一个呢?或者这被认为是一种好的做法,就像对单语句if语句使用大括号一样 - 如果稍后添加其他构造函数而你忘记了你没有默认值......?

java default-constructor

14
推荐指数
2
解决办法
4374
查看次数

类外的默认构造函数/析构函数?

以下是否符合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)

c++ constructor destructor default-constructor c++11

13
推荐指数
1
解决办法
7170
查看次数

"没有合适的默认构造函数" - 为什么甚至调用默认构造函数?

我已经看了一些关于这个问题的其他问题,但我不明白为什么在我的情况下甚至应该调用默认构造函数.我可以提供一个默认的构造函数,但我想了解它为什么这样做以及它会影响什么.

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++ constructor member default-constructor

13
推荐指数
2
解决办法
3万
查看次数

用户声明的默认构造函数+类内初始值设定项!=用户提供的构造函数?

锵文档整齐地解释说,

如果类或结构没有用户定义的默认构造函数,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 …

c++ default-constructor in-class-initialization c++11 c++14

13
推荐指数
1
解决办法
594
查看次数

为什么String类没有无参数构造函数?

intobject有一个无参数的构造函数.为什么不string呢?

.net c# string clr default-constructor

13
推荐指数
2
解决办法
2485
查看次数

未初始化的局部变量,默认为c ++ 11

打印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)

c++ default-constructor undefined-behavior c++11

13
推荐指数
1
解决办法
2677
查看次数

显式默认默认构造函数和聚合

当我编译#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有什么区别?

c++ constructor default-constructor c++11 c++14

13
推荐指数
1
解决办法
468
查看次数

12
推荐指数
1
解决办法
298
查看次数

C++对象实例化与分配

这有什么区别:

TestClass t;
Run Code Online (Sandbox Code Playgroud)

还有这个:

TestClass t = TestClass();
Run Code Online (Sandbox Code Playgroud)

我希望第二个可能会调用构造函数两次然后调用operator =,而是调用构造函数一次,就像第一次一样.

c++ constructor default-constructor copy-assignment

12
推荐指数
1
解决办法
8072
查看次数

C++ constexpr继承构造函数

以下代码使用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)

c++ default-constructor language-lawyer constexpr clang++

12
推荐指数
2
解决办法
693
查看次数