我写了以下示例代码:
class MyClass {
static int a;
public:
MyClass ( int i ) : a ( i ) {
cout << " \n ctor called. a is : "<< a << " \n";
}
};
int MyClass::a = 1;
int main( ) {
MyClass my(2);
}
Run Code Online (Sandbox Code Playgroud)
我知道这会产生编译错误,因为静态数据成员不能在构造函数初始化列表中使用.
那么如何在每次创建类的对象时初始化静态数据成员?我希望从构造函数调用的静态成员函数可以做到这一点.这是唯一可能的方式吗?
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
std::string s = {123};
std::cout << s << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么这个程序打印{为输出?它只是打印前面的词法分析器中的错误{吗?
我用g ++ 4.8.1编译了这个(没有错误或警告).MSVC不会编译这种string不是聚合类型的抱怨.
我的问题是:例如我有两个类x和y
class X{
public:
X(int, int, string);
private: int a;
int b;
string c;
};
class Y{
private: X x[10];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何通过Y的构造函数初始化这个10 x对象的数组?初始化列表?如何在y中初始化x的这10个对象.
为了理解如何使用initializer_list,我正在编写一个自己的构造函数来填充整数向量(这里的解释):
#include <vector>
class X
{
std::vector< int > *vec;
public:
X(std::initializer_list<int>);
};
X(std::initializer_list<int> values)
{
this->vec = new std::vector<int>(values);
}
Run Code Online (Sandbox Code Playgroud)
这条线
X(std::initializer_list<int> values)
Run Code Online (Sandbox Code Playgroud)
我的g ++ -std = c ++ 11:在值之前无效的声明符被拒绝.为什么?
我有一个嵌套的地图,即map<int, map<int, string>>我想用初始化列表初始化.我可以使用初始化列表来初始化单级映射,但似乎无法找出嵌套映射的正确语法.它甚至可能吗?
MWE:
// This example shows how to initialize some maps
// Compile with this command:
// clang++ -std=c++11 -stdlib=libc++ map_initialization.cpp -o map_initialization
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
cout << "\nLearning map initialization.\n" << endl;
map<int, string> level1map = {
{1, "a"},
{2, "b"},
{3, "c"}
};
for (auto& key_value : level1map) {
cout << "key: " << key_value.first << ", value=" << key_value.second << endl;
}
// This section …Run Code Online (Sandbox Code Playgroud) 我对以下代码感到困惑:
#include <Eigen/Dense>
#include <vector>
class Foo {};
void f(Eigen::MatrixXd const &) {}
void f(std::vector<Eigen::MatrixXd> const &) {}
void g(Foo const &) {}
void g(std::vector<Foo> const &) {}
int main()
{
Foo a, b, c;
Eigen::MatrixXd x, y, z;
// f({x, y}); ambiguity, why?!
f({x, y, z}); // ok
g({a,b}); // ok
g({a,b,c}); // ok
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释中的第 3 行代码main(),我会收到一个模棱两可的调用错误,
/Users/vlad/so.cpp: In function 'int main()':
/Users/vlad/so.cpp:17:13: error: call of overloaded 'f(<brace-enclosed initializer list>)' is ambiguous
f({x, y}); //ambiguity, why?! …Run Code Online (Sandbox Code Playgroud) 我有一个这样的类,除了有多个成员:
struct S {
S( S&& s ) : member( std::move( s.member ) ) {}
std::vector< int > member;
};
Run Code Online (Sandbox Code Playgroud)
我基本上想要聚合初始化就像这样:
S s{ {} };
Run Code Online (Sandbox Code Playgroud)
但由于自定义构造函数,这似乎是不可能的,所以我想使用构造函数参数来模拟它:
struct S {
S( S&& s ) : member( std::move( s.member ) ) {}
S( std::vector< int >&& vec ) : member( std::move( vec ) ) {}
S( const std::vector< int >& vec ) : member( vec ) {}
std::vector< int > member;
};
Run Code Online (Sandbox Code Playgroud)
如果可能的话,这看起来像是一个很大的冗余,并且在其他方面进行复制,尤其是在排列数量激增的情况下,会有更多的成员.我想我想完美转发?
struct S {
template< typename V >
S( V&& …Run Code Online (Sandbox Code Playgroud) 当我正在阅读C++(第3版)之旅时,我使用complex该类(第4.2节)看到了以下示例.
在复杂的类定义中:
complex& operator+=(complex z)
{
re += z.re;
im += z.im;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
与类定义分开定义:
complex operator+(complex a, complex b)
{
return a += b;
}
complex operator-(complex a)
{
return { -a.real(), -a.imag() };
} // unary -
Run Code Online (Sandbox Code Playgroud)
有人可以帮我理解为什么在operator+实现中,作者会调用operator+=,这会改变正在进行调用的复数对象的值吗?如果我要评估快递a+b,我会期望值a被改变吗?
另外,对于这种情况operator-,我对声明感到困惑:
return { -a.real(), -a.imag() };
Run Code Online (Sandbox Code Playgroud)作者是否在return语句后使用初始化列表?如果是这样,可以在不指定正在初始化的对象的名称的情况下完成吗?
提前致谢.
我正在尝试创建继承自std :: vector的MyVector类(添加一些有用的方法).一切都很好,但无法使用_initializer_list_进行初始化:
std::vector<int> a = { 4, 2 }; // OK
MyVector<int> b = { 4, 2 }; // Error
Run Code Online (Sandbox Code Playgroud)
VS2015和gcc都不允许编译它:
error: could not convert '{2, 3, 4}' from '<brace-enclosed initializer list>' to 'MyVector<int>'
Run Code Online (Sandbox Code Playgroud)
那又怎样?我尝试使用_initializer_list_ param明确添加构造函数来解决问题(参见下面的代码),但为什么呢?为什么它不继承自std:vector?
template <class T>
class MyVector : public std::vector<T>
{
public:
// Why is this constructor needed???
MyVector(const std::initializer_list<T>& il)
: std::vector<T>(il)
{
}
};
Run Code Online (Sandbox Code Playgroud)
PS我不想添加这个构造函数,以避免编写任何其他构造函数...
C++标准是否说这std::initializer_list<T>是对本地匿名数组的引用?如果它说,那么我们永远不应该返回这样的对象.标准中的任何部分都这样说了吗?
另一个问题是,一个std::initializer_list<T>可变的基础对象?我试着修改它:
#include <initializer_list>
int main()
{
auto a1={1,2,3};
auto a2=a1;//copy or reference?
for(auto& e:a1)
++e;//error
for(auto& e:a2)
cout<<e;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但编译错误:错误:只读参考'e'的增量
如果我想更改initializer_list中的值,我该如何解决?
initializer-list ×10
c++ ×9
c++11 ×7
anonymous ×1
arrays ×1
composition ×1
eigen ×1
map ×1
mutable ×1
static-data ×1
stdvector ×1
syntax ×1