指针参考模式 - 常用?

发布于 2024-11-19 02:16:52 字数 1241 浏览 2 评论 0原文

在当前对象由其他包含的对象操作的系统中,当传递对当前对象的引用时,链接似乎会一直持续下去......没有任何结束(对于下面的代码,Car->; myCurrentComponent->myCar_Brake->myCurrentComponent->myCar_Brake->myCurrentComponent ....)。

ICarCar->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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终陌 2024-11-26 02:16:52

只要您了解这一点,并且在不跟踪遇到的对象的情况下不要尝试遍历对象图,在对象图中使用循环引用就没有根本问题。具体回答你的问题,对象之间的循环引用是相对常见的;例如,这就是双向链表的工作方式。

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.

缺⑴份安定 2024-11-26 02:16:52

尽管正如 Paul 提到的,循环引用没有问题,但上面的代码示例完全缺少封装,并且内存泄漏不安全。

允许这样的事情有意义吗?

Speed::Speed(Car& value)
{
  myCar_Speed = value;
  // WTF code below
  value->myCurrentComponent->myCar_Brake = NULL;
}

另外,

Car::Car()
{
  myBrake = new Brake(*this);
  mySpeed = new Speed(*this);
  //if Speed::Speed(Car&) throws an exception, memory allocated for myBrake will leak
  myCurrentComponent = myBrake;
}

如果没有某种资源管理器,切勿使用原始指针。

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?

Speed::Speed(Car& value)
{
  myCar_Speed = value;
  // WTF code below
  value->myCurrentComponent->myCar_Brake = NULL;
}

Also,

Car::Car()
{
  myBrake = new Brake(*this);
  mySpeed = new Speed(*this);
  //if Speed::Speed(Car&) throws an exception, memory allocated for myBrake will leak
  myCurrentComponent = myBrake;
}

Never use raw pointers without some kind of a resource manager.

表情可笑 2024-11-26 02:16:52

在不讨论 CarBreakSpeed 关系的实际对象结构的有效性的情况下,这种方法有一个小问题:它可以处于无效状态。

如果出现问题,则在此设置中,Car 实例 #1 可能有一个属于 CarBreak 实例#2代码>实例#3。双链表的一个普遍问题也是——架构本身会启用无效状态。当然,仔细选择可见性修饰符和良好的函数实现可以保证它不会发生。当它完成并且安全时,你就停止修改它,把它当作一个“黑匣子”,然后直接使用它,从而消除了把它搞砸的可能性。

但是,我个人建议避免使用允许高级、持续维护的代码处于无效状态的架构。双向链表是一种低级黑盒代码,很可能像以前一样不需要任何代码更改。您能说说您的CarBreakSpeed吗?

如果汽车BreakSpeed,并且BreakSpeed不会知道他们“拥有Car”,就不可能做出无效状态。当然,也可能不适合具体情况。

Without debating the validity of the actual object structure of the relation of Car, Break and Speed, 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 a Break instance#2 that belongs to a Car 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 and Speed?

If a Car had a Break and Speed, and Break and Speed would not know of their "owning Car", it would be impossible to make and invalid state. Of course, it might not suit the concrete situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文