C ++:将构造对象的引用传递给构造成员对象? [英] C++: Passing reference of constructing object to constructed member objects?

查看:119
本文介绍了C ++:将构造对象的引用传递给构造成员对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,请考虑以下课程:

  class Object 
{
public:
//构造函数
Object():
[初始化列表]
{
...
}
...
};

class Container
{
public:
Object A;
对象B;

....

容器():
[初始化清单]
{
}
};

我想向对象提供[访问Container及其成员]。



我的第一个想法是以某种方式将对当前Container对象的引用传递给Objects的构造函数。但我无法弄清楚如何做到这一点。



我已经弄乱了这个,但我没有得到任何有用的东西。我试过这样的事情:

  class Object 
{
public:
Container& c

//构造函数
对象(容器& c_):
c(c_)
{
...
}
...
};

class Container
{
public:
Object A;
对象B;

....

Container():
A(对象(this))
B(对象(this))
{
}
};

我最终的目标是能够从对象A的成员方法中访问对象B. / p>

有没有人对如何更接近我正在寻找的东西有任何见解?



谢谢!

解决方案

UB 不一定要使用这个在初始化列表中,虽然需要小心,并且您的代码完全有效,只需稍加修改。

  class Container; 
class Object
{
public:
Container& c

//构造函数
对象(容器& c_):
c(c_)
{
}
};

class Container
{
public:
Object A;
对象B;

容器():
A(对象(* this))
B(对象(* this))
{
}
};

这个是一个指针,你想要一个参考,一个简单的去参考将做的伎俩。这是完全合法和定义的代码。不允许通过指针访问任何成员数据或函数,因为这些成员数据或函数在init列表完成之前可能还不存在。但它肯定 允许在它的初始化列表中获取对象的指针或引用并传递它。


Okay, consider the following classes:

class Object
{
public:
    // Constructor
    Object() : 
        [Initialization List]
    {
        ...
    }
    ...
};

class Container
{
public:
    Object A;
    Object B;

    ....

    Container() :
        [Initialization List]
    {
    }
};

I'd like to provide [access to Container and it's members] to the Objects.

My first thought was to somehow pass a reference to the current Container object to the constructors of the Objects. But I can't figure out how to do this.

I've messed around with "this", but I'm not getting anything that works. I tried something like this:

class Object
{
public:
    Container& c

    // Constructor
    Object(Container& c_) : 
        c(c_)
    {
        ...
    }
    ...
};

class Container
{
public:
    Object A;
    Object B;

    ....

    Container() :
        A(Object(this))
        B(Object(this))
    {
    }
};

My eventual goal is to be able to access Object B from inside a member method of Object A.

Does anyone have any insight on how to get closer to what I'm looking for?

Thanks!

解决方案

It is not UB or bad, necessarily, to use this in an initializer list, although care is needed, and your code is perfectly valid with minor modification.

class Container;
class Object
{
public:
    Container& c

    // Constructor
    Object(Container& c_) : 
        c(c_)
    {
    }
};

class Container
{
public:
    Object A;
    Object B;

    Container() :
        A(Object(*this))
        B(Object(*this))
    {
    }
};

this is a pointer, you wanted a reference, and a simple de-reference will do the trick. This is perfectly legal and defined code. What's not allowed is to access any member data or functions through the pointer, because those member data or functions simply may not exist yet until the init list is finished. But it definitely is allowed to take a pointer or reference to an object during it's initializer list and pass it around.

这篇关于C ++:将构造对象的引用传递给构造成员对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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