如何处理在构造this指针? [英] How to handle 'this' pointer in constructor?

查看:134
本文介绍了如何处理在构造this指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有创建它们的构造函数中的其他子对象,传递这个所以孩子可以节省指针回到它的父对象。我使用boost :: shared_ptr的广泛在我的编程作为一个更安全的替代标准:: auto_ptr的或原始指针。因此,儿童将不得不code,如的shared_ptr&LT;家长和GT; ,并提升提供了<一个href=\"http://boost.org/doc/libs/release/libs/smart_ptr/enable_shared_from_this.html\"><$c$c>shared_from_this()方法,家长可以给孩子。

I have objects which create other child objects within their constructors, passing 'this' so the child can save a pointer back to its parent. I use boost::shared_ptr extensively in my programming as a safer alternative to std::auto_ptr or raw pointers. So the child would have code such as shared_ptr<Parent>, and boost provides the shared_from_this() method which the parent can give to the child.

我的问题是, shared_from_this()不能在构造函数中,这是不是一个真正的犯罪,因为这个不应该在构造函数反正除非使用使用你知道你在做什么,不介意的限制。

My problem is that shared_from_this() cannot be used in a constructor, which isn't really a crime because 'this' should not be used in a constructor anyways unless you know what you're doing and don't mind the limitations.

谷歌的C ++风格指南<一个href=\"http://google-styleguide.google$c$c.com/svn/trunk/cppguide.xml?showone=Doing_Work_in_Constructors#Doing_Work_in_Constructors\">states该构造应该仅仅设置成员变量的初始值。任何复杂的初始化应该在明确的init()方法。这解决了这,在构造的问题,以及其他一些为好。

Google's C++ Style Guide states that constructors should merely set member variables to their initial values. Any complex initialization should go in an explicit Init() method. This solves the 'this-in-constructor' problem as well as a few others as well.

让我困扰的是,现在使用code的人一定记得调用的init()每次他们构建你的对象之一。我能想到的强制执行此的唯一方式是由具有断言的init()已经在每一个成员函数的顶部调用,但这是繁琐的编写和繁琐的执行。

What bothers me is that people using your code now must remember to call Init() every time they construct one of your objects. The only way I can think of to enforce this is by having an assertion that Init() has already been called at the top of every member function, but this is tedious to write and cumbersome to execute.

是否存在,在前进的道路上?

Are there any idioms out there that solve this problem at any step along the way?

推荐答案

使用一个工厂方法来两相结构和放大器;初始化类,然后使构造函数和放大器; init()函数私有。那么有没有办法不正确地创建对象。只要记得把析构函数公众和使用智能指针:

Use a factory method to 2-phase construct & initialize your class, and then make the ctor & Init() function private. Then there's no way to create your object incorrectly. Just remember to keep the destructor public and to use a smart pointer:

#include <memory>

class BigObject
{
public:
    static std::tr1::shared_ptr<BigObject> Create(int someParam)
    {
        std::tr1::shared_ptr<BigObject> ret(new BigObject(someParam));
        ret->Init();
        return ret;
    }

private:
    bool Init()
    {
        // do something to init
        return true;
    }

    BigObject(int para)
    {
    }

    BigObject() {}

};


int main()
{
    std::tr1::shared_ptr<BigObject> obj = BigObject::Create(42);
    return 0;
}

编辑:

如果你想反对住在栈中,你可以使用上述模式的变体。由于写了这将创建一个临时的,并使用拷贝构造函数:

If you want to object to live on the stack, you can use a variant of the above pattern. As written this will create a temporary and use the copy ctor:

#include <memory>

class StackObject
{
public:
    StackObject(const StackObject& rhs)
        : n_(rhs.n_)
    {
    }

    static StackObject Create(int val)
    {
        StackObject ret(val);
        ret.Init();
        return ret;
    }
private:
    int n_;
    StackObject(int n = 0) : n_(n) {};
    bool Init() { return true; }
};

int main()
{
    StackObject sObj = StackObject::Create(42);
    return 0;
}

这篇关于如何处理在构造this指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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