今天我在创建一个struct包含大量数据的时候遇到了这个问题.这是一个例子:
public struct ExampleStruct
{
public int Value { get; private set; }
public ExampleStruct(int value = 1)
: this()
{
Value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
看起来很好,花花公子.问题是当我尝试使用此构造函数而未指定值并希望对参数使用默认值1时:
private static void Main(string[] args)
{
ExampleStruct example1 = new ExampleStruct();
Console.WriteLine(example1.Value);
}
Run Code Online (Sandbox Code Playgroud)
此代码输出0但不输出1.原因是所有结构都具有公共参数构造函数.所以,就像我调用this()我的显式构造函数一样,在Main同样的事情发生在new ExampleStruct()实际调用ExampleStruct()但不调用的地方ExampleStruct(int value = 1).因为它这样做,它使用int的默认值为0 Value.
更糟糕的是,我的实际代码是检查该int value = 1参数是否在构造函数中的有效范围内.将其添加到ExampleStruct(int value = 1)上面的构造函数中:
if(value < 1 || …Run Code Online (Sandbox Code Playgroud) 我不明白为什么在foobar下面我需要指定std::vector<int>{}而在foobar2我不需要:
#include <iostream>
#include <memory>
#include <vector>
#include <tuple>
std::tuple<std::unique_ptr<int>, std::vector<int>> foobar() {
std::unique_ptr<int> test = std::make_unique<int>(42);
return { std::move(test), {} }; // <= this is a syntax error
// return { std::move(test), std::vector<int>{} } // <= this compiles...
}
std::tuple<int, std::vector<int>> foobar2() {
return { {}, {} };
}
int main() {
std::cout << *std::get<0>(foobar()) << "\n";
std::cout << std::get<0>(foobar2()) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
来自 GCC 的错误消息是
<source>: In function 'std::tuple<std::unique_ptr<int, std::default_delete<int> …Run Code Online (Sandbox Code Playgroud) 这与当前的MSVC编译器完美编译:
struct Foo
{
} const foo;
Run Code Online (Sandbox Code Playgroud)
但是,它无法使用当前的g ++编译器进行编译:
error: uninitialized const 'foo' [-fpermissive]
note: 'const struct Foo' has no user-provided default constructor
Run Code Online (Sandbox Code Playgroud)
如果我自己提供默认构造函数,它可以工作:
struct Foo
{
Foo() {}
} const foo;
Run Code Online (Sandbox Code Playgroud)
这是MSVC的另一个案例是过于宽松,还是g ++过于严格?
使用运算符 new() 创建类 C 的新对象会在此处出现错误:
class C
{
public:
C() {}
virtual ~C() {}
void operator delete(void*) = delete;
};
int main()
{
C* c = new C;
}
Run Code Online (Sandbox Code Playgroud)
和 C2280: 'void C::operator delete(void *)': function was explicitly deleted
但是,当我更换C() {}
使用C() = default;
或删除线,使编译器插入一个默认的构造函数(我相信有同样的效果= default),该代码将编译并运行。
使这种情况发生的编译器生成的默认构造函数和用户定义的默认构造函数之间有什么区别?
我在这篇文章中得到了一些提示,但是这里的 C 类(没有用户提供的构造函数)并不是微不足道的,因为析构函数是虚拟的,对吧?
使用最新的 Visual Studio,c++17 编译。
为什么我们在许多Java相关API中需要一个默认的无参数构造函数?像一般规则一样,所有java bean类或实体类(JPA等)或JAX-WS实现类都需要显式的无参数构造函数.
如果默认情况下Java提供了无参数构造函数,那么为什么这些标准中的大多数需要显式构造函数?
我正在编辑一些使用如下定义的全局数组的旧C++代码:
int posLShd[5] = {250, 330, 512, 600, 680};
int posLArm[5] = {760, 635, 512, 320, 265};
int posRShd[5] = {765, 610, 512, 440, 380};
int posRArm[5] = {260, 385, 512, 690, 750};
int posNeck[5] = {615, 565, 512, 465, 415};
int posHead[5] = {655, 565, 512, 420, 370};
Run Code Online (Sandbox Code Playgroud)
我想让所有这些数组成为下面定义的Robot类的私有成员.但是,当我声明数据成员时,C++编译器不允许我初始化数据成员.
class Robot
{
private:
int posLShd[5];
int posLArm[5];
int posRShd[5];
int posRArm[5];
int posNeck[5];
int posHead[5];
public:
Robot();
~Robot();
};
Robot::Robot()
{
// initialize arrays
}
Run Code Online (Sandbox Code Playgroud)
我想在Robot()构造函数中初始化这六个数组的元素.除了逐个分配每个元素之外,还有什么方法可以做到这一点吗?
我发誓我不记得以前见过这个,我很难相信自己的眼睛:
非聚合类的隐式定义的默认构造函数是初始化其成员还是否?
在Visual C++中,当我运行这个看似无辜的代码时......
#include <string>
struct S { int a; std::string b; };
int main() { return S().a; }
Run Code Online (Sandbox Code Playgroud)
...令我惊讶的是,它返回一个非零值!但如果我删除字段b,则返回零.
我已经在VC++的所有版本上尝试了这个,我可以得到它,它似乎在所有这些上都这样做.
但是当我在Clang和GCC上尝试它时,无论我是在C++ 98模式还是C++ 11模式下尝试它,都会将值初始化为零.
什么是正确的行为?它不能保证为零吗?
c++ constructor initialization default-constructor visual-c++
我遇到一个问题,询问"关于"默认"构造函数,以下哪一项是正确的?"
和一个选项"它初始化类的实例成员." 是不正确的选择.
现在我的理解是,如果我们有一个代码,如
Class Test {
String name;
}
Run Code Online (Sandbox Code Playgroud)
然后编译器创建看起来像的默认构造函数
Class Test {
String name;
Test(){
super();
name = null;
}
}
Run Code Online (Sandbox Code Playgroud)
是不是默认构造函数初始化实例成员name = null?
为什么我不能使用这样的代码?
<?php
class NoConstructor {
}
class ChildWithConstructor extends NoConstructor {
public function __construct() {
parent::__construct();
// do something
}
}
$foo = new ChildWithConstructor();
// **Fatal error: Cannot call constructor in file.php on line 8**
Run Code Online (Sandbox Code Playgroud)
例如.Java类有默认值,没有args构造函数.即使没有明确定义,也可以调用它.
当我们从父类中删除没有args构造函数时,PHP行为会导致麻烦,例如.当我们认为不再需要它时.
有谁知道为什么PHP创建者这样做了?
我遇到了一个问题,因为
std::is_trivially_default_constructible<std::pair<T1,T2>>::value == false;
Run Code Online (Sandbox Code Playgroud)
即使
std::is_trivially_default_constructible<T1>::value == true;
std::is_trivially_default_constructible<T2>::value == true;
Run Code Online (Sandbox Code Playgroud)
我没有找到这个设计的充分理由.如果有的话,拥有std::pair<T1,T2>一个=default构造函数是不合适的?T1T2
有一个简单的工作(比定义我自己更简单pair<>)?