在 c++ 中根据时间创建对象
我正在写一个关于车辆制造的模拟,我想知道如何根据时间创建对象。
我有一个基类车辆,以及子摩托车、汽车和卡车。 每1小时生产1辆摩托车, 每3小时将生产1辆汽车, 每 8 小时将生产 1 辆卡车。
我如何根据指定的时间创建这些对象?
谢谢
i am writing a simulation about vehicle manufacturing, i am wondering how i can create objects based on time..
i have a base class Vehicle, and children Motorbike, Car, and Truck.
1 Motorbike will be manufactured every 1 hour,
1 car will be manufactured every 3 hours,
and 1 truck will be manufactured every 8 hours.
how can i create those objects according to the time indicated?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种可能性是有一个线程专用于每个任务,它只是位于
泡沫、冲洗、创建、睡眠、重复的循环中。One possibility is to have a thread dedicated to each task, that just sits in a loop of
lather, rinse,create, sleep, repeat.您可以创建计时器并等待这些计时器事件。
当计时器到期时您可以创建相应的对象。
您可以在线程中监视它。
你什么时候销毁这些物体?
You can create timers and wait on those timer events.
When the timer expires you can create corresponding object.
You can monitor this in a thread.
When will you be destroying these objects?
如果您想完全控制时间(例如,您可以增加时间而不考虑系统计时器),那么您需要将其实现为一个类。然后提供一个单例或静态函数来返回当前时间。时间类应该是可复制的,以便对象可以记住开始时间。另外,提供将时间提前一定量并进行时间比较的功能。
在每个可制造对象(或者更好的是制造它们的工厂)内,添加两个函数:
StartManufacture
。这个函数应该记住开始时间。检查制造完成
。此函数获取当前时间,并检查自制造开始以来是否已经过了所需的等待时间。If you want to have complete control over the timing (say, you can increment the time irrespective of the system timer), then you'll need to implement that as a class. Then provide a Singleton or static function to return the Current time. The time class should be copyable so that objects can remember the starting time. Also, provide a function for advancing time by a certain amount, and for doing time comparisons.
Inside each manufacturable object (or better, the factories that manufacture them), add two functions:
StartManufacture
. This function should remember the starting time.CheckManufactureComplete
. This function fetches the current time, and checks if the required waiting time has elapsed since manufacturing started.