【发布时间】:2013-07-14 23:38:12
【问题描述】:
首先让我在这个问题前加上以下几点: 1)我已经在 Stackexchange 上搜索过这个问题,提供的大部分代码对我来说都很难理解,以保证提出一个新问题/打开一个新线程。我能找到的最接近的是Creating multiple class objects with the same name? c++,不幸的是,这超出了我的理解范围
2) http://www.cplusplus.com/doc/tutorial/classes/ 没有真正讨论过这个,或者我错过了。
现在已经不碍事了:
矩形类代码:
class Rectangle {
private:
int lineNumber;
float valueMax;
float valueMin;
public:
Rectangle(SCStudyInterfaceRef sc, int lineNumber, float valueMax, float valueMin);
int getLineNumber(); // member function of class
float getValueMax(); // member function of class Rectangle
float getValueMin(); // member function of class Rectangle
};
Rectangle::Rectangle(SCStudyInterfaceRef sc, int lineNumber0, float value1, float value2) {
lineNumber = lineNumber0;
int value2_greater_than_value1 = sc.FormattedEvaluate(value2, sc.BaseGraphValueFormat, GREATER_OPERATOR, value1, sc.BaseGraphValueFormat);
if (value2_greater_than_value1 == 1) {
valueMax = value2;
valueMin = value1;
} else {
valueMax = value1;
valueMin = value2;
}
}
int Rectangle::getLineNumber() {
return lineNumber;
}
float Rectangle::getValueMax() {
return valueMax;
}
float Rectangle::getValueMin() {
return valueMin;
}
这里是更重要的部分,这段代码几乎在循环中运行,并且每次某个事件触发它时都会重复:
bool xxx = Conditions here
if (xxx) {
// Draw new rectangle using plattforms code
code here
// Save rectangle information in the list:
Rectangle rect(sc, linenumbr + indexvalue, high, low);
(*p_LowRectanglesList).push_back(rect);
}
bool yyy = conditions here
if (Short) {
// Draw new rectangle using plattforms code
code here
// Save rectangle information in the list:
Rectangle rect(sc, linenumber + indexvalue, high, low);
(*p_HighRectanglesList).push_back(rect);
}
所以问题如下:
由于每次触发事件时都会循环运行代码的第二部分,因此将检查布尔条件,如果它为真,它将使用平台集成代码绘制一个矩形。绘制完成后,此信息将被传递给基于代码第一部分中的矩形类的新矩形对象/实例,使用:Rectangle rect(sc, linenumber + indexvalue, high, low); 部分,然后将该信息保存在列表中代码的不同部分暂时不相关。
当有一个新的 Bool = True 条件并且代码在它已经执行之后被执行时,究竟会发生什么?是否将旧的矩形对象简单地替换为具有相同名称并使用新参数的新矩形对象(因为由于代码的编写方式,它们在每个实例上都会发生变化)?或者现在有两个使用相同名称“rect”的 Rectangle 类对象?
从技术上讲,这对我来说甚至没有那么重要,因为无论如何都应该使用代码的(*p_HighRectanglesList).push_back(rect); 部分将参数信息推送到列表中
所以 TL;DR: “rect”会被破坏/覆盖,还是现在可能有无限数量的称为“rect”的矩形对象/实例漂浮在周围?
我为文字墙道歉,但作为一个完整的菜鸟,我认为最好概述我的思维过程,以便您更容易纠正我的错误。
亲切的问候, 轨道
【问题讨论】:
标签: c++ class object block instantiation