且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

初始化一个类数组?

更新时间:2022-02-23 21:35:52

这是一个完整的C ++ CON唯一的应用程序示例,演示我正在尝试

做什么。挑战是将InnerValue更改为数组并找出如何为数组的每个元素调用构造函数


TIA - Bob


#include" stdafx.h"

#include< iostream>

using namespace std;


class CInner {

public:

//构造函数

CInner(int x):Value(x){}


//会员变量

int价值;

};


类COuter {

public:

CInner InnerValue;


//构造函数

COuter():
InnerValue(2)

{}

};


int _tmain(int argc,_TCHAR * argv [])

{

COuter x;

cout<< x.InnerValue.Value<< endl;

返回0;

}
Here is a complete C++ console app example that demonstrates what I''m trying
to do. The challenge is to change InnerValue to an array and figure out how
to call the constructor for each element of the array.

TIA - Bob

#include "stdafx.h"
#include <iostream>
using namespace std;

class CInner {
public:
// Constructor
CInner(int x) : Value(x) {}

// Member variable
int Value;
};

class COuter {
public:
CInner InnerValue;

// Constructor
COuter() :
InnerValue(2)
{}
};

int _tmain(int argc, _TCHAR* argv[])
{
COuter x;
cout << x.InnerValue.Value << endl;
return 0;
}


好吧,我找到了一种方法可以得到我的意思在我之后。而不是尝试
有一个类实例数组,我可以创建一个指向类

实例的指针数组。这样我就可以使用新字样了。运算符在

构造函数的主体中初始化数组。当然,这意味着我需要

查看我的代码并更改所有相关的。。运营商到 - >

运营商,但在这种情况下这很容易做到。


但我仍然很好奇至于如果类没有默认构造函数,是否可以初始化一个

类的数组。
Well, I found a way to get kind of what I''m after. Instead of trying to
have an array of class instances, I can create an array of pointers to class
instances. That way I can use the "new" operator in the body of the
constructor to initialize the array. Of course, this means that I need to
go through my code and change all of the relevant "." operators to "->"
operators, but that''s easily enough done in this case.

But I''m still curious as to whether or not it''s possible to initialize an
array of classes if the class doesn''t have a default constructor.


Bob Altman写道:
Bob Altman wrote:

大家好,


我有一个包含成员变量的类,该变量是一个数组class

实例:


class MyClass {

private:

SomeClass m_someClass;

SomeClass m_arrayOfClasses [2];

};


现在,SomeClass类没有默认(无参数)构造函数,

所以我需要在我的MyClass

构造函数的初始化列表中调用它的构造函数,有点像这样:


/ /构造函数

MyClass :: MyClass(void):

//这个编译好了

m_someClass(constructor_arguments),


//这不编译

m_arrayOfClasses [0](constructor_arguments),

m_arrayOfClasses [1](constructor_arguments)

{}


如何初始化类实例数组?
Hi all,

I have a class that contains a member variable that is an array of class
instances:

class MyClass {
private:
SomeClass m_someClass;
SomeClass m_arrayOfClasses[2];
};

Now, the SomeClass class doesn''t have a default (paramterless) constructor,
so I need to call its constructor in my initialization list of the MyClass
constructor, kind of like this:

// Constructor
MyClass::MyClass(void) :
// This compiles ok
m_someClass( constructor_arguments ),

// This doesn''t compile
m_arrayOfClasses[0]( constructor_arguments ),
m_arrayOfClasses[1]( constructor_arguments )
{}

How do I initialize the array of class instances?



Bob:


如果SomeClass是可复制的,你可以做到


MyClass :: MyClass():

m_someClass(constructor_arguments)

{

m_arrayOfClasses [0] = SomeClass(constructor_arguments0);

m_arrayOfClasses [1] = SomeClass(constructor_arguments1);

}


-

David Wilkinson

Visual C ++ MVP

Bob:

If SomeClass is copyable you can do

MyClass::MyClass():
m_someClass( constructor_arguments )
{
m_arrayOfClasses[0] = SomeClass( constructor_arguments0 );
m_arrayOfClasses[1] = SomeClass( constructor_arguments1 );
}

--
David Wilkinson
Visual C++ MVP