このコード:
template <typename T>
struct A
{
T t;
void DoSomething()
{
t.SomeFunction();
}
};
struct B
{
};
A<B> a;
を呼び出さない限り、問題なく簡単にコンパイルできますa.DoSomething()
。
However, if I define DoSomething
as a virtual function, I will get a compile error saying that B
doesn't declare SomeFunction
. I can somewhat see why it happens (DoSomething should now have an entry in the vtable), but I can't help feeling that it's not really obligated. Plus it sucks.
Is there any way to overcome this?
EDIT 2: Okay. I hope this time it makes sence: Let's say I am doing intrusive ref count, so all entities must inherit from base class Object. How can I suuport primitive types too? I can define:
template <typename T>
class Primitive : public Object
{
T value;
public:
Primitive(const T &value=T());
operator T() const;
Primitive<T> &operator =(const T &value);
Primitive<T> &operator +=(const T &value);
Primitive<T> &operator %=(const T &value);
// And so on...
};
を使うことができます...しかし、どうPrimitive<int>
ですか?フロートには演算子がないため、問題のようです。しかし、実際にはそうではありません。これは、テンプレートの意図的な機能の 1 つです。Primitive<char>
Primitive<float>
%=
operator %=
Primitive<float>
何らかの理由で、operator %=
仮想と定義する場合。Primitive<float>
または、リンク エラーを回避するために dll から事前にエクスポートする場合operator %=
、Primitive<float>
. operator %=
inPrimitive<float>
の vtableにダミーの値を入力するだけであれば(例外が発生しますか?)、すべて問題ありませんでした。