[cppll:1189] <tips>何でも入るコンテナ
- Subject:
- [cppll:1189] <tips>何でも入るコンテナ
- From:
- mars <ms2@...>
- Date:
- Mon, 11 Feb 2002 01:22:00 +0900
- X-Mailer:
- EdMax Ver2.32.8F
- Message-Id:
ま〜ズです。こんばんは。
コンテナにオブジェクトを格納するとその厳密な型が
無くなってしまうのが嫌だったので、こんなのを考えました。
おまけに継承関係の無い型も一緒に格納できます。
(おまけの方がインパクトあります(^^;))
# 一般的な技術だったりするのでしょうか…。よくわからないので
# ここで聞いてみよう(笑)
長文です。すみませぬ。
//ここから
#include <vector>
#include <iostream>
struct iAdapter{
virtual void output() = 0;
virtual void zoom(int n) = 0;
virtual ~iAdapter(){}
};
template<typename T>
class Adapter : public iAdapter{
typedef T OriginalType;
public:
Adapter(T* t) : m_p(t){}
void output(){ hide::output(*this); } //ここではまだ型情報は生きている
void zoom(int n){ hide::zoom(*this, n); }
T* get(){return m_p;}
T& operator*(){return *m_p;}
T* operator->(){return m_p;}
private:
T* m_p;//実体を入れてもいいのかも。
};
namespace hide{
void output(Adapter<Point>& p)
{std::cout << "point : " << p->x << " : " << (*p).y << std::endl;}
void output(Adapter<int>& i)
{std::cout << "int : " << *(i.get()) << std::endl;}
template<typename T>//試しにこういうのも…
void zoom(Adapter<Point>& p, T n){
p->x *= n;
p->y *= n;
}
void zoom(Adapter<int>& i, int n){(*i) *= n;}
}
typedef std::auto_ptr<iAdapter> spItem;
typedef std::vector<spItem> Items; //ホントは危険。
int main(int argc, char* argv[]){
int i = 3;
Point p(10, 20);//int型でx,yを内包した構造体
Items items;
items.push_back( spItem(new Adapter<int>(&i)) );
items.push_back( spItem(new Adapter<Point>(&p)) );
Items::iterator c = items.begin();
for(; c != items.end(); ++c){
(*c)->output();
(*c)->zoom(4);//各値を4倍
(*c)->output();
}
return 0;
}
//ここまで
# メールは結局サポートから音沙汰無し。再々登録…。
コンテナにオブジェクトを格納するとその厳密な型が
無くなってしまうのが嫌だったので、こんなのを考えました。
おまけに継承関係の無い型も一緒に格納できます。
(おまけの方がインパクトあります(^^;))
# 一般的な技術だったりするのでしょうか…。よくわからないので
# ここで聞いてみよう(笑)
長文です。すみませぬ。
//ここから
#include <vector>
#include <iostream>
struct iAdapter{
virtual void output() = 0;
virtual void zoom(int n) = 0;
virtual ~iAdapter(){}
};
template<typename T>
class Adapter : public iAdapter{
typedef T OriginalType;
public:
Adapter(T* t) : m_p(t){}
void output(){ hide::output(*this); } //ここではまだ型情報は生きている
void zoom(int n){ hide::zoom(*this, n); }
T* get(){return m_p;}
T& operator*(){return *m_p;}
T* operator->(){return m_p;}
private:
T* m_p;//実体を入れてもいいのかも。
};
namespace hide{
void output(Adapter<Point>& p)
{std::cout << "point : " << p->x << " : " << (*p).y << std::endl;}
void output(Adapter<int>& i)
{std::cout << "int : " << *(i.get()) << std::endl;}
template<typename T>//試しにこういうのも…
void zoom(Adapter<Point>& p, T n){
p->x *= n;
p->y *= n;
}
void zoom(Adapter<int>& i, int n){(*i) *= n;}
}
typedef std::auto_ptr<iAdapter> spItem;
typedef std::vector<spItem> Items; //ホントは危険。
int main(int argc, char* argv[]){
int i = 3;
Point p(10, 20);//int型でx,yを内包した構造体
Items items;
items.push_back( spItem(new Adapter<int>(&i)) );
items.push_back( spItem(new Adapter<Point>(&p)) );
Items::iterator c = items.begin();
for(; c != items.end(); ++c){
(*c)->output();
(*c)->zoom(4);//各値を4倍
(*c)->output();
}
return 0;
}
//ここまで
# メールは結局サポートから音沙汰無し。再々登録…。