我想知道如何将一个对象的引用存储在另一个对象中,并将该引用设置为私有属性。示例(伪代码):
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 类中。我知道如何简单地将它带入一个方法并在该方法的范围内使用它,但在这里我想将它设置为私有属性。我该怎么做呢?