如何从另一个类内部访问一个类
How do I access a class from inside another class?
作业的下一部分告诉我,类RacingCar
包含由类 Wheel 定义的四个轮子对象。将轮子实现为堆上的对象数组。
class RacingCar{
int speed;
public:
void Accelerate(int value) {
speed = speed + value;
}
void Brake(int value) {
speed = speed - value;
}
void Turn(int value) {
speed = value / 4;
}
void Print(){
cout << "The current KM/h of the car is: " << speed;
}
};
class Wheel {
int *ptrSize;
int pressure;
public:
Wheel() : pressure(32) {
ptrSize = new int(30);
}
Wheel (int s, int p) : pressure(p) {
ptrSize = new int(s);
}
~Wheel() {
delete ptrSize;
}
void pump(int amount) {
pressure += amount;
}
void print() {
cout << pressure;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Wheel *heapArray = new Wheel[4];
RacingCar F1; //Creating a "Formula 1" test car
//Test parameters
F1.Accelerate(10);
F1.Brake(50);
F1.Turn(180);
F1.Print(); //Checks to see if the car changes actually worked
getch();
delete [] heapArray; //Delete when you're done
return 0;
}
构建这样的类,然后在RacingCar
内部访问它是否理想?还是有更好的方法?否则,我看不到在堆上创建它的方法。
将轮子实现为堆上的对象数组。
真的有点毫无意义,但这是一种练习,所以...
作业告诉我,"赛车"类包含由类轮定义的四个轮子对象。
当你的作业说"包含四个轮子对象"时,你可以将此要求翻译成"轮子对象应该是我的类的成员"。
我假设你的教练希望你在赛车类的ctor中初始化轮子对象数组,然后在赛车类的dtor中释放(delete[]
)它。
但是请注意,执行此操作的正确方法是:
class Wheel;
class RacingCar {
...
std::vector<Wheel> wheels; // if you need a variable amount of wheels
Wheel wheels[4]; // if you need exactly 4 wheels.
...
即使你真的必须在堆上分配 Wheel 对象,你仍然不会使用 delete[]
,但最好使用 boost::scoped_array
这样的工具。
让我们充实一下,因为我们想要完整的答案,即使是"家庭作业"问题,对吧?
如上所述,建模包含通常是通过类的成员完成的,尽管在这种情况下,类的成员可能是某种数组。
鉴于在堆上分配 Wheel 的要求(虽然对于玩具示例没有意义,但在成员场景中有很多合法的用例可以在堆上分配类的对象)——我会说以下解决方案是好的风格:
这为您提供了一个正好包含 4 个堆分配对象的数组。您不需要显式释放这些内容:
class RacingCar { ... boost::scoped_array<Wheel> wheels; ... RacingCar() : wheels(new Wheel[4]) { } ...
这使用 4 个单独的成员,这在某些用例中可能有意义,在这些用例中,您拥有同一类的成员,但不要统一使用它们:
class RacingCar { ... boost::scoped_ptr<Wheel> front_left; boost::scoped_ptr<Wheel> front_right; boost::scoped_ptr<Wheel> rear_left; boost::scoped_ptr<Wheel> rear_right; ... RacingCar() : front_left(new Wheel) , front_right(new Wheel) , rear_left(new Wheel) , rear_right(new Wheel) { }
如果要使用可变大小,则需要执行以下操作:
class RacingCar { ... boost::ptr_vector<Wheel> wheels; ... RacingCar() { for(size_t i=0; i<4; ++i) { wheels.push_back(new Wheel); } } ...
最后,如果你没有提升但普通的C++,我会这样做:(哦,请注意,在第一个版本中我忘记添加复制 ctor 运算符。这就是您不弄乱原始指针并删除的原因。你永远会忘记sth。;-)
class RacingCar { ... Wheel* wheels; ... RacingCar() : wheels(new Wheel[4]) { } ~RacingCar() { delete[] wheels; } private: // Block copy operations. These would need to be // implemented properly for the wheels member. RacingCar(RacingCar const&); // no impl. RacingCar& operator=(RacingCar const&); // no impl. ...
赋值意味着 Wheel
的数组应该是类 RacingCar
的一部分,例如
class RacingCar {
public:
...
private:
...
Wheel *wheels;
};
你应该在构造函数中分配它,然后在析构函数中销毁它。
将"*heapArray"添加为RacingCar的属性并在其构造函数中创建轮子。
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 如何创建一个函数来提取向量内部字符串中的字段?
- 在另一个结构中声明内部结构会导致错误:结构使用无效
- 在VC++中从DLLMAIN内部调用D3D的CREATEDEVICE时,它会创建一个死锁(loaderlock?)。有没有办法克服这个问题?最终目标内
- 将一个函数名称传递给模板,以使用模板主体内部的几个过载
- C++:在循环内部或外部声明一个向量
- 使用 c++ 从另一个内部菜单返回到主菜单
- 是一个被认为是不同类型的班级内部的正向声明
- 当指针作为指向另一个功能内部指针的指针传递时会发生什么
- 如何查找查看一个字符串是否位于另一个字符串内部并替换它
- 为 c++ 构建一个静态库,该库在内部依赖于第三方库
- 初始化指向另一个结构内部结构的指针
- 返回一个在函数内部填充的constchar*向量是定义良好的行为吗
- 在外部功能中具有一个变量初始化或内部功能中的多个初始化是最有效的
- 为什么我可以选择*不*调用并发::代理::d一个内部运行
- 声明一个内部有指针的联合指针
- 构造一个内部有结构体的类
- RealSense投影::CreateColorImageMappedToDepth返回Null,如果其中一个内部参数通
- Calloc (), malloc() vs新建一个内部有unordered_map的结构体