【发布时间】:2013-11-10 12:43:31
【问题描述】:
自定义类型的动态数组有什么特别需要注意的地方吗?
我正在尝试创建一个 ConditionParameter 的动态数组(定义如下)
ConditionParameter* m_params;
...
m_params = new ConditionParameter[m_numParams];
但是上面一行的结果只是一个ConditionParameter类型的新对象,其地址存储在m_params中。
struct ConditionParameter
{
ConditionParameter() :
type(OBJ_TYPE_OBJECT),
semantic(OP_SEMANTIC_TYPE_NONE),
instance(NULL),
attrib(ATTRIB_TYPE_NONE),
value(0)
{}
ConditionParameter(const ConditionParameter& other)
{
attrib = other.attrib;
instance = other.instance;
semantic = other.semantic;
type = other.type;
value = other.value;
}
ConditionParameter& operator = (ConditionParameter& other)
{
attrib = other.attrib;
instance = other.instance;
semantic = other.semantic;
type = other.type;
value = other.value;
return *this;
}
ObjectType type;
OperandSemanticType semantic;
Object* instance;
AttributeType attrib;
int value;
};
【问题讨论】:
-
“只是一个新对象” - 不正确。您可以通过在构造函数中打印一些内容来验证这一点。
-
@albizgil,调试器不知道 m_params 是如何创建的。他只知道这是一个 ConditionParamter* 并且只会将其解释为这样。
-
改用
std::vector! -
我无法正确回忆,但我相信您可以通过在 watch/var 窗口中使用
variable,len语法来指定数组计数以扩展像这样的块分配序列. IE。输入m_params, N,其中N是您分配的元素数。我可能在语法上错了,但我知道这个功能就在那里。 -
@WhozCraig:你是对的,请参阅 related question for Visual Studio。