解释C ++代码 [英] Explain C++ code
本文介绍了解释C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以从以下代码的解释中获得帮助吗?
#include< iostream>
class Vector {
private:
double ∗ elem; //指向元素
int sz的指针; //元素数量
public:
Vector(int s):elem {new double [s]},sz {s} {}
double& operator [](int i){return elem [i]; }
int size(){return sz; }
};
我正在尝试提高我的C ++知识,但是这种语法对我来说似乎很新。 p>
更具体地说,是 public
下的代码。
解决方案
也许是新的C ++ 11初始化列表使您感到困惑,现在您可以使用大括号 {}
初始化变量。例如:
int i {42};
std :: vector< int> v {1、2、3、4};
代码中的所有其他内容在C ++ 11之前看起来都是很标准的
Can I get some help with explanation of the following code?
#include <iostream>
class Vector {
private:
double∗ elem; // pointer to the elements
int sz; // the number of elements
public:
Vector(int s) :elem{new double[s]}, sz{s} { }
double& operator[](int i) { return elem[i]; }
int size() { return sz; }
};
I am trying to brush up my C++ knowledge, but this syntax seems very new to me.
More specifically the code under public
.
解决方案
Maybe it's the new C++11 initialisation-lists that are confusing you, you now can initialise a variable with curly-braces {}
. For example:
int i{42};
std::vector<int> v{1, 2, 3, 4};
everything else in your code looks pretty standard pre-C++11
这篇关于解释C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文