C ++中的循环引用,无指针 [英] Circular reference in C++ without pointers

查看:152
本文介绍了C ++中的循环引用,无指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来定义循环引用而不使用指针?

Is there a way to define circular references without using pointers?

我需要这样的东西:

struct A;
struct B {
    A a;
};

struct A {
    B b;
};


$ b

谢谢!

Thanks!

推荐答案

您可以使用引用

struct A;
struct B {
  A& a;
};

struct A {
  B b;
};

但是没有一个级别的间接创建一个循环引用是不可能的。你的样例所做的甚至不是创建一个循环引用,它试图创建一个递归定义。结果将是无限大小的结构,因此不是合法的。

But no it's not possible to create a circular reference without some level of indirection. What your sample is doing is not even creating a circular reference, it's attempting to create a recursive definition. The result would be a structure of infinite size and hence not legal.

这篇关于C ++中的循环引用,无指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆