gpt4 book ai didi

c++ - 自己的智能指针的模板特化

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:05 25 4
gpt4 key购买 nike

我有自己的智能指针类实现。

template<class Pointee>
class SmartPtr {
private:
Pointee* _pointee;
SmartPtr(SmartPtr &);
public:
explicit SmartPtr(Pointee * pt = 0);
~SmartPtr();
SmartPtr& operator=(SmartPtr&);
operator Pointee*() const { return _pointee; }
bool operator!() const { return _pointee != 0; }
bool defined() const { return _pointee != 0; }
Pointee* operator->() const { return _pointee; }
Pointee& operator*() const { return *_pointee; }
Pointee* get() const { return _pointee; }
Pointee* release();
void reset(Pointee * pt = 0);
};

template<class Pointee>
class SmartPtr<SmartPtr<Pointee>> {
private:
Pointee* _pointee;
SmartPtr(SmartPtr &);
public:
explicit SmartPtr(SmartPtr<Pointee> * pt = 0);
~SmartPtr();
SmartPtr& operator=(SmartPtr&);
operator Pointee*() const { return *_pointee; }
bool operator!() const { return _pointee != 0; }
bool defined() const { return _pointee != 0; }
Pointee& operator->() const { return _pointee; }
Pointee& operator*() const { return *_pointee; }
Pointee* get() const { return _pointee; }
Pointee* release();
void reset(Pointee * pt = 0);
};

template<class Pointee>
SmartPtr<Pointee>::SmartPtr(SmartPtr &spt) :_pointee(spt.release()) {
return;
}

template<class Pointee>
SmartPtr<Pointee>::SmartPtr(Pointee * pt) : _pointee(pt) {
return;
}

template<class Pointee>
SmartPtr<SmartPtr<Pointee>>::SmartPtr(SmartPtr<Pointee>* pt) : _pointee(pt) {
return;
}

template<class Pointee>
SmartPtr<Pointee>::~SmartPtr() {
delete _pointee;
}

template<class Pointee>
SmartPtr<Pointee>& SmartPtr<Pointee>::operator=(SmartPtr &source)
{
if (&source != this)
reset(source.release());
return *this;
}

template<class Pointee>
Pointee * SmartPtr<Pointee>::release() {
Pointee* oldSmartPtr = _pointee;
_pointee = 0;
return oldSmartPtr;
}

template<class Pointee>
void SmartPtr<Pointee>::reset(Pointee * pt) {
if (_pointee != pt)
{
delete _pointee;
_pointee = pt;
}
return;
}

这个想法是我可以做到这一点:

SmartPtr<SmartPtr<SmartPtr<Time>>> sp3(new SmartPtr<SmartPtr<Time>>(new SmartPtr<Time>(new Time(0, 0, 1))));

Time 这是我自己的测试类。它有方法 hours(),它在控制台中显示我在构造函数中设置的小时数。

我可以像这样在控制台中显示小时数:

cout << sp2->hours() << endl;

代替:

cout << sp3->operator->()->operator->()->hours() << endl;

我可以这样做,因为我有头等舱,在 opertor->() 中我返回 Pointee*

template<class Pointee>
class SmartPtr {...}

template<class Pointee>
class SmartPtr<SmartPtr<Pointee>> {...}

opertor->() 中我返回 Pointee &

但有些错误我无法修复。

Error C2440 initialization: can not be converted "SmartPtr<Time> *" in "Time *" 
Error C2439 SmartPtr<SmartPtr<Time>>::_pointee: unable to initialize member

最佳答案

如果你真的想获得一个operator->()递归地提取 SmartPtr获取内部编号 SmartPtr指针...我认为你应该避免专门化 SmartPtr类,对于 SmartPtr模板参数,但你必须“专门化” operator->()

我的意思是...

(1) 删除SmartPtr<SmartPtr<Pointee>>特化

(2) 在 SmartPtr 之前实现,开发一个类型特征来检测是否PointeeSmartPtr , 举个例子

template <typename>
class SmartPtr;

template <typename>
struct isSP : public std::false_type
{ };

template <typename P>
struct isSP<SmartPtr<P>> : public std::true_type
{ };

(3) 要求真正执行operator->()到您通过标签调度选择的另一个功能,例如

auto const operator->() const
{ return getPtr(isSP<Pointee>{}); }

(4) 实例实现不同版本的派发函数,在 true 的情况下递归,在 false 的情况下返回指针

auto const getPtr (std::true_type const &) const 
{ return _pointee->operator->(); }

auto const getPtr (std::false_type const &) const
{ return _pointee; }

现在(在 main() 中)你可以写 `

std::cout << sp3->hours() << std::endl;

(但你确定这是个好主意吗?)

无论如何...不幸的是这个解决方案使用了auto getPtr() 的返回类型和 operator->()所以这只适用于 C++14;如果您想要 C++11(或 C++98)解决方案,那就有点复杂了。

下面是一个完整的工作示例

#include <iostream>
#include <type_traits>

template <typename>
class SmartPtr;

template <typename>
struct isSP : public std::false_type
{ };

template <typename P>
struct isSP<SmartPtr<P>> : public std::true_type
{ };

template<class Pointee>
class SmartPtr {
private:
Pointee* _pointee;
SmartPtr(SmartPtr &);
public:
explicit SmartPtr(Pointee * pt = 0);
~SmartPtr();
SmartPtr& operator=(SmartPtr&);
operator Pointee*() const { return _pointee; }
bool operator!() const { return _pointee != 0; }
bool defined() const { return _pointee != 0; }


auto const getPtr (std::true_type const &) const
{ return _pointee->operator->(); }

auto const getPtr (std::false_type const &) const
{ return _pointee; }

auto const operator->() const
{ return getPtr(isSP<Pointee>{}); }


Pointee& operator*() const { return *_pointee; }
Pointee* get() const { return _pointee; }
Pointee* release();
void reset(Pointee * pt = 0);
};

template<class Pointee>
SmartPtr<Pointee>::SmartPtr(SmartPtr &spt) :_pointee(spt.release()) {
return;
}

template<class Pointee>
SmartPtr<Pointee>::SmartPtr(Pointee * pt) : _pointee(pt) {
return;
}

template<class Pointee>
SmartPtr<Pointee>::~SmartPtr() {
delete _pointee;
}

template<class Pointee>
SmartPtr<Pointee>& SmartPtr<Pointee>::operator=(SmartPtr &source)
{
if (&source != this)
reset(source.release());
return *this;
}

template<class Pointee>
Pointee * SmartPtr<Pointee>::release() {
Pointee* oldSmartPtr = _pointee;
_pointee = 0;
return oldSmartPtr;
}

template<class Pointee>
void SmartPtr<Pointee>::reset(Pointee * pt) {
if (_pointee != pt)
{
delete _pointee;
_pointee = pt;
}
return;
}


struct Time
{
int a, b, c;

Time (int a0, int b0, int c0) : a{a0}, b{b0}, c{c0}
{ }

int hours () const
{ return a; }
};


int main ()
{
SmartPtr<SmartPtr<SmartPtr<Time>>>
sp3(new SmartPtr<SmartPtr<Time>>(new SmartPtr<Time>(new Time(0, 0, 1))));

std::cout << sp3->hours() << std::endl;
}

关于c++ - 自己的智能指针的模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46625073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com