C++;纯虚函数模板类的语法? 我只熟悉C++模板,而对模板类添加纯虚函数已经耗尽了满足编译器的能力。
下面的代码C++;纯虚函数模板类的语法? 我只熟悉C++模板,而对模板类添加纯虚函数已经耗尽了满足编译器的能力。,c++,templates,virtual,pure-virtual,C++,Templates,Virtual,Pure Virtual,下面的代码 #include <iostream> #include <string> #include <sstream> class Pearl { public: Pearl(int value); virtual ~Pearl(); protected: int mValue; }; Pearl::Pearl(int value) : mValue(value) {
#include <iostream>
#include <string>
#include <sstream>
class Pearl
{
public:
Pearl(int value);
virtual ~Pearl();
protected:
int mValue;
};
Pearl::Pearl(int value)
: mValue(value)
{
std::cout << "$" << mValue << " Pearl created" << std::endl;
}
Pearl::~Pearl()
{
std::cout << "$" << mValue << " Pearl destroyed" << std::endl;
}
///////////////////////////////////////////////////////////////////////
class Oyster
{
public:
Oyster(std::string str, int value);
virtual ~Oyster();
protected:
Pearl mPearl;
std::string mName;
friend class OStreamer;
};
Oyster::Oyster(std::string name, int value)
: mName(name)
, mPearl(value)
{
std::cout << "Oyster " << mName << " created" << std::endl;
}
Oyster::~Oyster()
{
std::cout << "Oyster " << mName << " destroyed" << std::endl;
}
///////////////////////////////////////////////////////////////////////
template <typename T> class Streamer
{
public:
Streamer(T& rT, unsigned int flags);
~Streamer();
virtual std::ostream Display() = 0;
static const unsigned int A = 0x1;
static const unsigned int B = 0x2;
protected:
T& mrT; // Aah pity the foo!
unsigned int mFlags;
friend class OStreamer;
};
///////////////////////////////////////////////////////////////////////
class OStreamer : public Streamer<Oyster>
{
public:
OStreamer(Oyster oyster, unsigned int flags);
virtual std::ostream Display();
};
OStreamer::OStreamer(Oyster oyster, unsigned int flags)
: Streamer<Oyster>(oyster, flags)
{
}
std::ostream OStreamer::Display()
{
std::ostringstream oss;
oss << "Oyster[" << mrT.mName << "]" << std::endl;
}
///////////////////////////////////////////////////////////////////////
template <typename T> std::ostream& operator<<(std::ostream& os, const Streamer<T> streamer)
{
return os;
}
///////////////////////////////////////////////////////////////////////
int main()
{
Oyster sam("Sam", 50);
std::cout << OStreamer(sam, OStreamer::A) << std::endl;
return 0;
}
#包括
#包括
#包括
珍珠级
{
公众:
珍珠(整数值);
虚拟珍珠();
受保护的:
int值;
};
珍珠:珍珠(整数)
:mValue(值)
{
std::cout将操作员过载更改为:
template <typename T> std::ostream& operator<<(std::ostream& os,
const Streamer<T> &streamer)
{
return os;
}
template std::ostream&operator我敢打赌。+1D'oh!我盯着代码看了太久,完全掩盖了这一点。我谦恭地接受我公开的尴尬。:)谢谢你,@sam varshavchik!
template <typename T> std::ostream& operator<<(std::ostream& os,
const Streamer<T> &streamer)
{
return os;
}