如何创建一个类来保存另一个类的对象数组,如何操作其他类的私有属性
How to create a class that holds array of objects of another class , how to manipulate the other class private attributes
// this is the first point class header
class Point : public CWaypoint{
public: //temporarily
string m_description;
public:
Point();
virtual ~Point();
void print();
Point(string name, string description, double latitude, double longitude);
void getAllDataByReference( string& name,string& description, double& latitude,double& longitude);
};
// This is the database class header
class Database
{
private:
Point m_POI[10]; // Point is the other class
int m_noPoi;
public:
Database();
virtual ~Database();
void addPoI(string name,string description,double latitude,double longitude);
Point * getPointerToPoi(string name);
}
首先,要添加到数组中,您可以依赖于隐式声明的复制赋值操作符,该操作符对所有字段进行浅层复制:
void Database::addPoI(string name,string description,double latitude,double longitude) {
if (m_noPoi >= 10) // handle error
m_POI[m_noPoi++] = Point(name, description, latitude, longitude);
}
由于数据是私有的,你不能直接访问它(忽略"友"船)。但是,您可以通过point::getAllDataByReference()
读取数据(通过它的副本)。
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 重载操作程序时出错>>用于类中的字符串 memebr
- 函数何时会在c++中包含stack_Unwind_Resume调用
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- 对字符串进行位操作
- MSVC是否支持C++11样式的属性而不是__declspec
- 如何根据自定义操作中的属性集安装功能
- 如何通过 COM 互操作访问复杂对象的属性?
- 如何在自定义操作DLL(MSI / Wix)中获取"INSTALLED"属性?
- 对 Qi 中解析器公开的属性应用操作
- 如何创建一个可以对同一类的多个对象进行操作的函数,访问C++中的私有属性
- 可视化如何从C++自定义操作获取 MSI 'UILevel'属性
- Windows安装程序,在C++中访问自定义操作中的自定义属性
- 为传递到qi::phrase_parse的表达式设置语义操作的属性
- 如何创建一个类来保存另一个类的对象数组,如何操作其他类的私有属性
- 从语义操作返回的值会干扰规则属性
- 如何在MITK中访问图像属性列表和操作