初始化“数组”的默认构造函数中的值班级
当我尝试使用初始值的默认构造函数打印出该数组内的值时,它都是随机垃圾数。
我以为默认构造函数会自动将值分配给 (0,0),但似乎并非如此。我想知道我应该做什么。
// default constructor that allocates 10 elements
Array::Array()
{
m_size = 10;
m_data = new Point[m_size];
}
这是我的班级私人成员:
class Array {
private:
int m_size;
Point* m_data;
另外,如果我尝试将冒号语法设置为 Array::Array():m_size(10),m_data(new Point[10])
它仍然给我随机数。
When I try to print out the value inside this array with the default constructor of the initial value, it's all random garbage numbers.
I thought the default constructor would automatically assign the value into (0,0), but it seems like it's not. I was wondering what I should do.
// default constructor that allocates 10 elements
Array::Array()
{
m_size = 10;
m_data = new Point[m_size];
}
Here is my private member for the class:
class Array {
private:
int m_size;
Point* m_data;
Also, if I try to do a colon syntax asArray::Array():m_size(10),m_data(new Point[10])
it's still giving me random number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像这样初始化数组:
这也适用于初始化列表:
You can initialize the array like this:
This also works in the initializer list: