指针参考模式 - 常用?
在当前对象由其他包含的对象操作的系统中,当传递对当前对象的引用时,链接似乎会一直持续下去......没有任何结束(对于下面的代码,Car->; myCurrentComponent->myCar_Brake->myCurrentComponent->myCar_Brake->myCurrentComponent
....)。
ICar
和 Car->myCurrentComponent->myCar_Brake
引用相同的地址,指向相同的对象。就像Car包含Brake一样,它指的是Car。
事实上,Car是唯一的对象,myCar_Brake和myCar_Speed只是引用(指向)它。这种引用和指针的使用正常吗?这种方法有什么潜在的问题吗?
示例代码
class Brake
class C
class Car
{
public:
Car();
// Objects of type B and C.
Brake* myBrake;
Speed* mySpeed;
// Current component under action.
Component* myCurrentComponent;
}
/******************************/
// Constructor
Car::Car()
{
myBrake = new Brake(*this);
mySpeed = new Speed(*this);
myCurrentComponent = myBrake;
}
/******************************/
class Brake: public Component
{
public:
Brake(Car&);
// Needs to operate on A.
Car* myCar_Brake;
}
// Constructor
Brake::Brake(Car&)
{
myCar_Brake = Car;
}
/******************************/
class Speed
{
public:
Speed(Car&);
// Needs to operate on A.
Car* myCar_Speed;
}
// Constructor
Speed::Speed(Car&)
{
myCar_Speed = Car;
}
/****************************/
In a system where current object is operated by other contained objects, when reference to current object is passed, it appears that the link goes on and on....without any end ( For the code below, Car->myCurrentComponent->myCar_Brake->myCurrentComponent->myCar_Brake->myCurrentComponent
....).
ICar
and Car->myCurrentComponent->myCar_Brake
refer to same address, point to same objects. It's like Car contains Brake which refers to Car.
In fact, Car is the only object, myCar_Brake and myCar_Speed just refer(point) to it.Is this kind of use of reference and pointer normal? Are there any potential problem with this approach?
Sample Code
class Brake
class C
class Car
{
public:
Car();
// Objects of type B and C.
Brake* myBrake;
Speed* mySpeed;
// Current component under action.
Component* myCurrentComponent;
}
/******************************/
// Constructor
Car::Car()
{
myBrake = new Brake(*this);
mySpeed = new Speed(*this);
myCurrentComponent = myBrake;
}
/******************************/
class Brake: public Component
{
public:
Brake(Car&);
// Needs to operate on A.
Car* myCar_Brake;
}
// Constructor
Brake::Brake(Car&)
{
myCar_Brake = Car;
}
/******************************/
class Speed
{
public:
Speed(Car&);
// Needs to operate on A.
Car* myCar_Speed;
}
// Constructor
Speed::Speed(Car&)
{
myCar_Speed = Car;
}
/****************************/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要您了解这一点,并且在不跟踪遇到的对象的情况下不要尝试遍历对象图,在对象图中使用循环引用就没有根本问题。具体回答你的问题,对象之间的循环引用是相对常见的;例如,这就是双向链表的工作方式。
There's no fundamental problem with having circular references in your object graph, so long as you understand that and don't try to traverse your object graph without keeping track of which objects you've encountered. To specifically answer your question, having circular references between objects is relatively common; it's the way a doubly-linked list works, for example.
尽管正如 Paul 提到的,循环引用没有问题,但上面的代码示例完全缺少封装,并且内存泄漏不安全。
允许这样的事情有意义吗?
另外,
如果没有某种资源管理器,切勿使用原始指针。
Although, as Paul mentions, there is no problem with having circular references, the above code example is totally missing encapsulation and is not memory leak safe.
Does it make sense to allow something like this?
Also,
Never use raw pointers without some kind of a resource manager.
在不讨论
Car
、Break
和Speed
关系的实际对象结构的有效性的情况下,这种方法有一个小问题:它可以处于无效状态。如果出现问题,则在此设置中,
Car
实例 #1 可能有一个属于Car
Break 实例#2代码>实例#3。双链表的一个普遍问题也是——架构本身会启用无效状态。当然,仔细选择可见性修饰符和良好的函数实现可以保证它不会发生。当它完成并且安全时,你就停止修改它,把它当作一个“黑匣子”,然后直接使用它,从而消除了把它搞砸的可能性。但是,我个人建议避免使用允许高级、持续维护的代码处于无效状态的架构。双向链表是一种低级黑盒代码,很可能像以前一样不需要任何代码更改。您能说说您的
Car
、Break
和Speed
吗?如果
汽车
有Break
和Speed
,并且Break
和Speed
不会知道他们“拥有Car
”,就不可能做出无效状态。当然,也可能不适合具体情况。Without debating the validity of the actual object structure of the relation of
Car
,Break
andSpeed
, this approach has one minor problem: it can be in invalid states.If - something - goes wrong, it is possible in this setup, that a
Car
instance#1 has aBreak
instance#2 that belongs to aCar
instance#3. A general problem with doubly-linked lists too - the architecture itself enables invalid states. Of course careful visibility modifier choosing and good implementation of functions can guarantee it will not happen. And when its done and safe, you stop modifying it, take it as a 'black box', and just use it, thus eliminating the probability of screwing it up.But, I'd personally recommend to avoid architectures that allow invalid states for high level, constantly maintained code. A doubly-linked list is a low level balck box code that will most likely not need any code changes, like ever. Can you say that about your
Car
,Break
andSpeed
?If a
Car
had aBreak
andSpeed
, andBreak
andSpeed
would not know of their "owningCar
", it would be impossible to make and invalid state. Of course, it might not suit the concrete situation.