gpt4 book ai didi

c++ - C++中自定义默认构造函数和默认默认构造函数有什么区别

转载 作者:行者123 更新时间:2023-11-30 04:43:30 24 4
gpt4 key购买 nike

我正在开发一个程序,当我将默认构造函数设置为省略它时,它的行为会有所不同。具体来说,我正在构建一个对象以将其传递给第三方库函数。当我省略默认构造函数时,函数会按预期工作,但是当我定义它时,函数会阻塞并且不会返回控制权,所以我认为问题出在构造函数上。

下面我展示了与问题有关的代码部分的摘要摘录。

class Config {
public:
typedef std::shared_ptr<Config> SPtr;

int frame;
int numSolidParticles = 0;
bool shapesChanged = false;

Buffer* buffer;
FlexParams params; // struct

// if omitted then the 3rd party method works as expected
Config() {
}
};

...

Config::SPtr config = std::make_shared<Config>();

initialize(config);

...
SetFlexParams(config->params);
// calls to other 3rd party methods that depends on config
...

// call 3rd party library's method that causes the issue
mapBuffer(config->buffer);

更新:我正在尝试做的是移动一些初始化(例如 framebuffer 和其他未显示的字段)在方法 inititalize 到构造函数中。

最佳答案

What is the difference between a custom default constructor and defaulted default constructor?

给定一个类 X,如果:( [class.ctor]/5 )

  • X is a union that has a variant member with a non-trivial default constructor and no variant member of X has a default member initializer,

  • X is a non-union class that has a variant member M with a non-trivial default constructor and no variant member of the anonymous union containing M has a default member initializer,

  • any non-static data member with no default member initializer is of reference type,

  • any non-variant non-static data member of const-qualified type (or array thereof) with no brace-or-equal-initializer does not have a user-provided default constructor,

  • X is a union and all of its variant members are of const-qualified type (or array thereof),

  • X is a non-union class and all members of any anonymous union member are of const-qualified type (or array thereof),

  • any potentially constructed subobject, except for a non-static data member with a brace-or-equal-initializer, has class type M (or array thereof) and either M has no default constructor or overload resolution as applied to find M's corresponding constructor results in an ambiguity or in a function that is deleted or inaccessible from the defaulted default constructor, or

  • any potentially constructed subobject has a type with a destructor that is deleted or inaccessible from the defaulted default constructor.

然后 X 的默认默认构造函数被定义为已删除,而手写的 X() {} 将是格式错误的。

否则没有区别。

关于c++ - C++中自定义默认构造函数和默认默认构造函数有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58244942/

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