C++类中的对象引用

发布于 2024-12-05 04:22:40 字数 487 浏览 0 评论 0原文

我想知道如何将一个对象的引用存储在另一个对象内部,并将该引用设置为私有属性。示例(伪代码):

class foo
{
    public:
        int size;
        foo( int );
};

foo::foo( int s ) : size( s ) {}

class bar
{
    public:
        bar( foo& );
    private:
        foo fooreference;
};

bar::bar( foo & reference )
{
    fooreference = reference;
}

foo firstclass( 1 );
bar secondclass( firstclass );

正如您所看到的,我只想能够将 foo 的引用存储在这个 bar 类中。我知道如何简单地将它带入一个方法并仅在该方法的范围内使用它,但在这里我想将它设置为私有属性。我该怎么做呢?

I am wondering how to store a reference of an object inside of another object, and also set that reference as a private property. Example (pseudo-code):

class foo
{
    public:
        int size;
        foo( int );
};

foo::foo( int s ) : size( s ) {}

class bar
{
    public:
        bar( foo& );
    private:
        foo fooreference;
};

bar::bar( foo & reference )
{
    fooreference = reference;
}

foo firstclass( 1 );
bar secondclass( firstclass );

As you may be able to see, I just want to be able to store the reference of foo inside this bar class. I know how to simply bring it into a method and use it just in the scope of that method, but here I want to set it as a private property. How would I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

安静 2024-12-12 04:22:40

与定义和使用任何类成员的方式相同。

确保使用_member-initialiser初始化引用成员,而不是事后在构造函数主体中分配给它;请记住,引用必须被初始化并且以后不能被反弹。

class foo
{
    public:
        int size;
        foo( int );
};

foo::foo( int s ) : size( s ) {}

class bar
{
    public:
        bar(foo&);
    private:
        foo& fooreference;
};

bar::bar(foo& reference) : fooreference(reference)
{}

foo firstclass(1);
bar secondclass(firstclass);

The same way you define and use any class member.

Make sure you initialise the reference member with the _member-initialiser, instead of just assigning to it after-the-fact in the constructor body; recall that references must be initialised and cannot later be rebound.

class foo
{
    public:
        int size;
        foo( int );
};

foo::foo( int s ) : size( s ) {}

class bar
{
    public:
        bar(foo&);
    private:
        foo& fooreference;
};

bar::bar(foo& reference) : fooreference(reference)
{}

foo firstclass(1);
bar secondclass(firstclass);
白云不回头 2024-12-12 04:22:40
bar::bar( foo & reference )
{
    fooreference = reference;
}

fooreference 只是另一个对象。通过分配,您正在制作参考的副本。请注意,fooreference 不是 reference 的别名。

bar::bar( foo & reference )
{
    fooreference = reference;
}

fooreference is just another object. By assigning, you are making a copy of the reference. Note that fooreference isn't an alias to the reference.

作死小能手 2024-12-12 04:22:40

您无法重新设置引用,因此必须在初始值设定项列表中设置它。

struct Foo {};

struct Bar {
  Bar(Foo &foo_) : foo(foo_) {}
  void set(Foo &foo_) { foo = foo_; } // copies, doesn't reseat
  Foo &foo;
};

You cannot reseat a reference, so you have to set it in the initializer list.

struct Foo {};

struct Bar {
  Bar(Foo &foo_) : foo(foo_) {}
  void set(Foo &foo_) { foo = foo_; } // copies, doesn't reseat
  Foo &foo;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文