【发布时间】:2014-05-27 04:33:18
【问题描述】:
我在整个项目/类中继承了这个小实用程序类(模板化)。
这个想法是它允许轻松地将各种成员打包进出类实例(对于网络等,并不完全重要)
我得到的如下
template<typename T>
struct Packable
/**
* Packs a <class T> into a Packet (Packet << T)
* Required for chaining packet packing
*************************************************/
virtual sf::Packet& operator <<(sf::Packet& packet) = 0; // Work-horse, must be defined by child-class
friend sf::Packet& operator <<(sf::Packet& packet, const T *t)
{
// Call the actual one, but basically do nothing...
return packet << *t;
}
friend sf::Packet& operator <<(sf::Packet& packet, const T &t)
{
// Call the actual one, but basically do nothing...
return packet << &t;
}
friend sf::Packet& operator <<(sf::Packet& packet, T *t)
{
// Call the actual one, but basically do nothing...
return packet << *t;
}
friend sf::Packet& operator <<(sf::Packet& packet, T &t)
{
// Call the actual one, but basically do nothing...
return packet << &t;
}
};
我想要做的,简而言之,就是让它只需要在子类中指定/充实一种方法(用“虚拟”词表示)。
我想要的是提供采用各种形式的类的其他方法,并根据需要取消引用它们以使用编译类时将存在的虚拟方法。
问题是我似乎创建了一些无限循环。
friend sf::Packet& operator <<(sf::Packet& packet, T &t)
{
// Call the actual one, but basically do nothing...
return packet << &t;
}
只是一遍又一遍地调用自己。如何取消引用对其对象的引用?
【问题讨论】:
-
@RakibulHasan,
&t是t所指对象的地址。 -
你有一个没有结束条件的递归定义。您应该实现其中一个运算符。
-
@RakibulHasan 奥利? See it live.
-
@MohitJain 是的...正如我在 OP 中所述,我有一个递归定义。问题是,我该如何解决这个问题?我不能将
-
所有这些友元函数与抽象
Packable运算符 anything 是 Packable 或其中的派生;只是一堆使用sf:Packets 操作的友元方法。
标签: c++ templates inheritance operator-overloading dereference