java 智能指针_从原始指针/引用创建智能指针

我对C很新,并且有关于指针/引用的问题 . 以下示例反映了我的问题:

#include

#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()) {

}

explicit CorrectClient(const boost::shared_ptr 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 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;

}

正如评论所说, SegmentationFaultClient 类的实现是完全错误的,因为默认构造函数在堆栈上创建了一个对象,该对象被删除了"immediately" . 因此我提出了使用指针的类 CorrectClient . 我的目标是保持良好的API(没有公共Boost)来自 SegmentationFaultClient ( const& 默认构造函数) . 上面的示例确实 not 工作并终止,并出现以下错误:

invalid conversion from 'const UsedObjectInterface*' to 'boost::shared_ptr::element_type* {aka UsedObjectInterface*}' [-fpermissive]

explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete

所以我的问题是:是否可以将原始指针 * 转换为智能指针?如果是这样,最好的方法是什么?如果您发现我的代码有任何其他问题,请告诉我们!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值