2013-11-26 130 views
1

我对C++来说很新颖,并且在指针/引用方面存在问题。正如评论说,该SegmentationFaultClient类的implementaton根本就是错误的,因为默认的构造函数创建堆栈,这是“立即”取消了对对象从原始指针/参考创建智能指针

#include <iostream> 

#include "boost/make_shared.hpp" 
#include "boost/utility.hpp" 

class UsedObjectInterface { 
public: 
    virtual int data() const = 0; 
}; 

class UsedObject : public UsedObjectInterface { 
public: 
    UsedObject() : data_(42) { 
    } 

    explicit UsedObject(int value) : data_(value) { 
    } 

    int data() const { 
    return data_; 
    } 

private: 
    const int data_; 
}; 

class BaseClient : private boost::noncopyable { 
public: 
    virtual const UsedObjectInterface& used_object() const = 0; 
}; 

class SegmentationFaultClient : public BaseClient { 
public: 
    // This can't work, since the object is deleted immediately. 
    // IMHO only the following two solutions can work: 
    // 1. The member attribute is not a reference (not possible with an abstract class, we lose the advantages of polymorphism). 
    // 2. The member attribute is a pointer. 
    SegmentationFaultClient() : used_object_(UsedObject()) { 
    } 

    explicit SegmentationFaultClient(const UsedObjectInterface& used_object) 
     : used_object_(used_object) { 
    } 

    const UsedObjectInterface& used_object() const { 
    return this->used_object_; 
    } 

private: 
    const UsedObjectInterface& used_object_; 
}; 

class CorrectClient : public BaseClient { 
public: 
    CorrectClient() : used_object_(boost::make_shared<UsedObject>()) { 
    } 

    explicit CorrectClient(const boost::shared_ptr<UsedObjectInterface> used_object) 
     : used_object_(used_object) { 
    } 

    // TODO Is it possible to change this to a const&, so at least the interface 
    // is the same as in SegmentationFaultClient? Then the above constructor can 
    // be deleted. 
    explicit CorrectClient(const UsedObjectInterface& used_object) 
     : used_object_(&used_object) { 
     // TODO How-to convert a raw pointer to a smart pointer? 
    } 

    const UsedObjectInterface& used_object() const { 
    return *this->used_object_; 
    } 

private: 
    const boost::shared_ptr<UsedObjectInterface> used_object_; 
}; 

int main() { 
    SegmentationFaultClient segfault_client; 
    const UsedObjectInterface& a = segfault_client.used_object(); 
    std::cout << a.data() << std::endl; 

    // Correct, but how to make this work with a const& constructor? 
    const UsedObject first_object; 
    CorrectClient correct_client(first_object); 
    const UsedObjectInterface& b = correct_client.used_object(); 
    std::cout << b.data() << std::endl; 
} 

:下面的例子反映了我的问题。因此,我想出了使用指针的类CorrectClient。我的目标是保持从SegmentationFaultClientconst&默认构造函数)良好的API(没有公共升压)。上面的例子确实工作,并与下面的错误而终止:

invalid conversion from 'const UsedObjectInterface*' to 'boost::shared_ptr<UsedObjectInterface>::element_type* {aka UsedObjectInterface*}' [-fpermissive] 
explicit shared_ptr(Y * p): px(p), pn() // Y must be complete 

所以我的问题是:是否有可能一个原始指针*转换为智能指针?如果是这样,那么最好的方法是什么?如果您看到我的代码有任何其他问题,请让我知道!

回答

2

问题是,您试图将const-pointer转换为non-const pointer。您可以使用reference作为PARAM,而不是const-reference,或者你可以做以下

const boost::shared_ptr<const UsedObjectInterface> used_object_; 

然而,默认删除器为shared_ptrdelete指针,也就是在堆未分配你的情况。在这种情况下,您应指向empty-deleter,或不要使用shared_ptr

+0

哇,我真是一个愚蠢的错误。是的,我只制作了智能指针“const”,而不是实际的对象。谢谢你的提示!我不完全理解你的最后一句话,你说“point'empty-deleter'”是什么意思?你能提供更多的信息吗? –

+0

@FlorianWolters http://www.boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm使用构造函数与deleter,这是函数,什么都不做。 – ForEveR

+0

顺便说一句,你有什么建议来处理这种情况?两种方法都会创建“原始指针”行为。我想要一个默认的构造函数和一个完整的构造函数。默认的构造函数应该创建另一个类的实例,而完整的构造函数需要同一个类的一个参数。有没有“最佳实践”的C++内存所有权方法? –