从其他类访问向量的向量值
Accessing, and push_back values of a vector from a different class?
我总体上对如何访问向量并从其他类中添加值。
我想从其他类中添加值(向量是私有的)。我还想访问Main()中的向量并能够将其打印出来。
谁能给我一个例子说明这是如何完成的?
Class A
{
//vector is here - it's a private vector
}
Class B
{
//add values to the vector here
}
main()
{
//access the vector here, and print out the values
}
如果向量是A
的私人成员,则提供访问的通常方法是通过A
的公共成员函数将访问向量访问。通常,您应该仅揭示必要的访问权限。阅读有关封装的信息。如果您打算为其提供完整的公共接口,那么将矢量私有的意义没有太大的意义。如果您只希望B
类可以不受限制地访问它(以及A
的其他成员),则可以使B
成为A
的朋友类。
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- C++从另一个类访问公共静态向量的正确方法是什么
- 访问存储在向量C++中的结构的多态成员
- 如何从静态向量访问类元素?
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 从父类的向量访问子类函数,而无需向下转换
- 如何使用常数向量访问成员函数
- 如何从C 向量访问单个单词
- 从C++中传递给函数的指针向量访问矩阵
- 多向量访问与本地对象
- C 迭代器与用于向量访问的指针
- 从向量访问成员功能
- 如何在 c++ 中从 "pointer-to-objects" 向量访问对象
- 如何从双精度向量的向量访问数据
- 从指针向量访问成员函数?C++
- 如何使用SWIG Python从内部向量访问c ++对象方法
- 无法从向量访问对象的getter
- 从向量访问某些类函数C++的问题
- 从基类类型的向量访问派生类方法
- 从对象指针向量访问对象