这是一个OO概念吗? [英] Is this an OO concept?
问题描述
我使用名为Derived的模板类来实例化
类Abstract的派生类。这是一个OO概念吗?下面是一些代码来说明:
#include< iostream>
class Abstract
{
public:
virtual void func()const = 0;
virtual~Abstract(){}
};
模板< typename T>
类派生:公共摘要
{
T obj;
public:
Derived(){}
void func()const {obj.func( ); }
};
A级
{
公开:
void func()const
{std :: cout<< A :: func called <<的std :: ENDL; }
};
B级
{
公开:
void func()const
{std :: cout<< B :: func called <<的std :: ENDL; }
};
int main()
{
摘要* obj = new Derived< ; A> ;;
obj-> func();
obj = new Derived< B> ;;
obj- > func();
}
程序的输出是:
A :: func叫做
B :: func叫
谢谢。
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept? Here is some code to illustrate:
#include <iostream>
class Abstract
{
public:
virtual void func() const = 0;
virtual ~Abstract() { }
};
template <typename T>
class Derived : public Abstract
{
T obj;
public:
Derived() { }
void func() const { obj.func(); }
};
class A
{
public:
void func() const
{ std::cout << "A::func called" << std::endl; }
};
class B
{
public:
void func() const
{ std::cout << "B::func called" << std::endl; }
};
int main()
{
Abstract *obj = new Derived<A>;
obj->func();
obj = new Derived<B>;
obj->func();
}
The output of the program is:
A::func called
B::func called
Thanks.
推荐答案
On Sun,2005年6月19日09:39:07 +0400,Jason Heyes
< ja ******** @ optusnet.com.au>写道:
On Sun, 19 Jun 2005 09:39:07 +0400, Jason Heyes
<ja********@optusnet.com.au> wrote:
我使用名为Derived的模板类来实例化类Abstract的派生类。这是一个OO概念吗?
你为什么关心?
这里有一些代码可以说明:
#include< iostream>
类摘要
公共:
虚拟空虚func()const = 0;
虚拟〜摘要(){}
} ;
模板< typename T>
类派生:公共摘要
{t / b>公开:
Derived(){ }
void func()const {obj.func(); }
};
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept?
Why do you care?
Here is some code to illustrate:
#include <iostream>
class Abstract
{
public:
virtual void func() const = 0;
virtual ~Abstract() { }
};
template <typename T>
class Derived : public Abstract
{
T obj;
public:
Derived() { }
void func() const { obj.func(); }
};
派生模板是适配器设计模式。它使用对象组合
将Abstract接口添加到包含的对象中。它完全是OO。
-
Maxim Yegorushkin
Derived template is the adapter design pattern. It uses object composition
to add Abstract interface to the contained object. And it''s perfectly OO.
--
Maxim Yegorushkin
" Maxim Yegorushkin" < e - ***** @ yandex.ru>在消息中写道
news:op.sslslhogti5cme@home ...
"Maxim Yegorushkin" <e-*****@yandex.ru> wrote in message
news:op.sslslhogti5cme@home...
On Sun,2005年6月19日09:39:07 +0400,Jason Heyes
< ; JA ******** @ optusnet.com.au>写道:
On Sun, 19 Jun 2005 09:39:07 +0400, Jason Heyes
<ja********@optusnet.com.au> wrote:
我使用名为Derived的模板类来实例化类Abstract的派生类。这是一个OO概念吗?
为什么要关心?
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept?
Why do you care?
我想为我的班级找到正确的命名。
I would like to find the right naming for my classes.
这里有一些代码可以说明:
#include< iostream>
班级摘要
{
公开:> virtual void func()const = 0;
virtual~Abstract(){}
};
模板< typename T>
类派生:公共摘要
{obj;
公开:
Derived(){}
void func()const {obj.func() ; }
};
Here is some code to illustrate:
#include <iostream>
class Abstract
{
public:
virtual void func() const = 0;
virtual ~Abstract() { }
};
template <typename T>
class Derived : public Abstract
{
T obj;
public:
Derived() { }
void func() const { obj.func(); }
};
派生模板是适配器设计模式。它使用对象组合
将Abstract接口添加到包含的对象中。它完全是OO。
Derived template is the adapter design pattern. It uses object composition
to add Abstract interface to the contained object. And it''s perfectly OO.
好的。我最好将Derived重命名为Adapter。感谢您提供的信息。
Ok. I''d better rename Derived to Adapter. Thanks for the information.
Jason Heyes写道:
Jason Heyes wrote:
我使用一个名为Derived的模板类,用于实例化类Abstract的派生类。这是一个OO概念吗?
I use a template class called Derived to instantiate derived classes of
class Abstract. Is this an OO concept?
i希望你确实意识到只有Derived(或现在的适配器)是
_only_派生类的摘要。 A级和A级B r diff类。你好
模板类对他们必须拥有的对象施加限制
func()定义。我不会将这样的模板限制确定为OO
。相反,你可能想尝试:
class Abstract {
public:
virtual void func()const = 0;
};
班级来电者{
摘要* x;
公开:
Caller(){x = NULL; }
来电者(摘要* x){this-> x = x; }
~Calller(){if(x)delete x; }
void func(){if(x)x-> func(); }
void set_x(Abstract * x)
{if(this-> x)delete this-> x;
this- > x = x; }
//你可能想尝试过载da = operator
//但要记住< Caller obj> = new<摘要派生>不会
工作!
};
A类:公共摘要{
public:
void func()const {cout<< " A" << ENDL; }
};
B级:公共摘要{
public:
void func( )const {cout<< " B" << ENDL; }
};
int main()
{
来电者c(新A) ;
c.func();
c.set_x(新B);
c.func();
返回0;
}
i hope u do realize the fact that only Derived (or now Adapter) is the
_only_ derived class of Abstract. classes A & B r diff classes. ur
template class imposes the restriction on objects that they must have
func() defined. i wouldnt exactly attribute such template restrictions
to OO. instead u might wanna try:
class Abstract {
public:
virtual void func() const = 0;
};
class Caller {
Abstract *x;
public:
Caller() { x = NULL; }
Caller(Abstract *x) { this->x = x; }
~Caller() { if(x) delete x; }
void func() { if(x) x->func(); }
void set_x(Abstract *x)
{ if(this->x) delete this->x;
this->x = x; }
// u might wanna try overloadin da = operator
// but keep in mind that <Caller obj> = new <Abstract derived> won''t
work!
};
class A : public Abstract {
public:
void func() const { cout << "A" << endl; }
};
class B : public Abstract {
public:
void func() const { cout << "B" << endl; }
};
int main()
{
Caller c(new A);
c.func();
c.set_x(new B);
c.func();
return 0;
}
这篇关于这是一个OO概念吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!