gpt4 book ai didi

包含数组的 C++ 类

转载 作者:行者123 更新时间:2023-11-28 08:19:39 25 4
gpt4 key购买 nike

我正在启动一个 C++ 应用程序项目,该项目基本上有一个对象类作为保存数据的核心,然后一些函数将处理该对象的数据以获取所需的信息。我以前在 VB.net 中写过类似的东西,我以前用 C++ 写过小程序,但那是在 Turbo C++ 中,不包括创建我自己的类。我想用 VC++ 编写这个,我想在开始之前征求一些关于构建这个类的建议。当我在 VB.net 中完成类似操作时,我使用了将结构和函数嵌套在主结构中的方法。例如:

Public Structure struct_Tags      Public Structure struct_TagProps          Public Name As String          Public Value As String      End Structure      Public TagName As String      Public TagProperties() As struct_TagProps      Public Function HasProperties() As Boolean          HasProperties = False          If Not TagProperties Is Nothing Then              HasProperties = True          End If      End Function  End Structure  Dim obj_Tags() As struct_Tags

这种方法在 VB.net 中运行良好,我在需要时将 obj_Tags()obj_Tags().TagProperties() 动态调整为 +1我的处理循环。但是我的研究告诉我,在 C++ 中,我的类中不能有 TagProperties 结构的空数组变量。它是否正确?我无法提前知道数组边界是什么,那么如何设置这个类来动态改变数组边界呢?通过使用 vector ?只要我可以动态地向数组添加维度,我就可以使用 [0] 绑定(bind)初始化数组。那可能吗?在此先感谢您的帮助。

最佳答案

struct struct_Tags {
struct struct_TagProps {
string Name;
string Value;
};
string TagName;
vector<struct_TagProps> TagProperties;
bool HasProperties() const {
return !TagProperties.empty().
}
};
vector<struct_Tags> obj_Tags;

插入内容:

struct_Tags newValue;

struct_Tags::struct_TagProps newProps = { "my name", "my value" };
newValue.TagProperties.push_back(newProps);

obj_Tags.push_bcak(newValue);

关于包含数组的 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6328167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com