gpt4 book ai didi

C++ void* 任何类型的实现返回奇怪的结果

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:28 26 4
gpt4 key购买 nike

我正在尝试用 C++ (object) 实现一个基本的 any 类型实现,但如果我想得到,它总是打印 CCCCCCCC来自任何类型的值,这让我感到困惑,为什么(虽然我知道 void* 是危险的):

#include <typeinfo>
struct object
{
private:
template < typename T > struct _base
{
typedef T _ptr_type;
_ptr_type* _ptr_val()
{
return _ptr;
}
_base(_ptr_type value) : _ptr(&value){}
_base() : _ptr(nullptr){}
_ptr_type* _ptr;
};
struct _holder : _base<void*>
{
template < typename Ty > void cast(const _base<Ty>* p_base)
{
_ptr->~_ptr_type();
_ptr_type _n_type = (_ptr_type)p_base->_ptr, *_n_ptr = &_n_type;
std::swap<_ptr_type*>(_ptr, _n_ptr);
}
_holder(){}
};
public:
_holder* _h_ptr;
object() : _h_ptr(new _holder){}
template < typename T > object(const T& value) : _h_ptr(new _holder)
{
_base<T> _t_base(value);
_h_ptr->cast(&_t_base);
}
template < typename T > void operator=(const T& value)
{
_base<T> _t_base(value);
_h_ptr->cast(&_t_base);
}
const void* operator()() const
{
return *_h_ptr->_ptr_val();
}
};

#include <iostream>
int main()
{
object MyObject = 'c';
std::cout << MyObject();
getchar();
}

最佳答案

也许我对对象类的实现会对你有所帮助。它类似于 boost::any,但具有更多功能(operator==operator!=)

class object
{
private:
class dummy
{
public:
dummy()
{
}
virtual ~dummy()
{
}
virtual const std::type_info &type() const = 0;
virtual dummy *duplicate() const = 0;
virtual bool eq(object) = 0;
};

template < typename _Ty > class data : public dummy
{
public:
data()
{
}

data(const _Ty &_Value)
: __data(_Value)
{
}

~data()
{
}

const std::type_info &type() const
{
return typeid(_Ty);
}

data *duplicate() const
{
return new data<_Ty>(__data);
}

bool eq(object _Obj)
{
return _Obj.cast<_Ty>() == __data;
}

_Ty __data;
};

dummy *d;
public:
object()
{
}

template < typename _Ty > object(const _Ty &_Value)
: d(new data<_Ty>(_Value))
{
}

object(const object &_Obj)
: d(_Obj.d->duplicate())
{
}

~object()
{
if (!empty())
{
delete d;
}
}

const std::type_info &type() const
{
return (empty() ? typeid(void) : d->type());
}

object &operator=(object &_Rhs)
{
if (&_Rhs != this)
{
d = _Rhs.d->duplicate();
}
return *this;
}

object &swap(object &_Rhs)
{
std::swap(*this, _Rhs);
return *this;
}

template < typename _Ty > object &operator=(const _Ty &_Value)
{
d = new data<_Ty>(_Value);
return *this;
}

template < typename _Ty > _Ty cast() const
{
if (type() == typeid(_Ty))
{
return static_cast<data<_Ty> *>(d)->__data;
}
throw std::exception("Invalid cast type");
}

bool operator==(const object &_Rhs) const
{
return (type() == _Rhs.d->type() ? d->eq(_Rhs) : false);
}

template < typename _Ty > bool operator==(_Ty _Value) const
{
return (type() == typeid(_Ty) ? cast<_Ty>() == _Value : false);
}

bool operator!=(const object &_Rhs) const
{
return !(*this == _Rhs);
}

template < typename _Ty > bool operator!=(_Ty _Value) const
{
return !(*this == _Value);
}

bool empty() const
{
return !d;
}
};

恐怕就像boost::any一样,没有getter函数,而是一个cast函数。可以这样用

int main()
{
object o = 5;
object o = (std::string)"Hello\n"; // doesn't like arrays, must be wrapped in a class
std::cout << o.cast<std::string>().c_str();
}

关于C++ void* 任何类型的实现返回奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21390727/

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