隐式接口 [英] Implicit Interfaces

查看:60
本文介绍了隐式接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

班级界面

{

public:

virtual void DoItNow()= 0;

};


A级:公共接口

{

public:

void DoItNow(); //明确满足界面

};


B级

{

public:

void DoItNow(); //可以隐含地满足界面

};


//必须包装B才能让类看起来像接口:(

//

C级:公共接口

{

B rep;

public:

void DoItNow(){rep.DoItNow();}

};


void some_func(Interface * obj)

{

obj-> DoItNow();

}


void another_func()

{

A a; //明确是一个接口

B b; //不是一个明确的接口

C c; //包裹B将其用作界面

some_func(& a); //显然可以

some_func(& b); //错误!b不是来自界面

some_func(& c); //显然可以

}


如果第二次通话有效通过隐式接口,可以消除

包装器类C.我是否描述了

动态类型?是隐式接口是什么模板 -

only ;概念问题TS" (建议)暗示?我不认为

它是动态类型,因为我想在编译时知道

时间对象是否满足接口。

模板考虑类型,而接口考虑行为。

隐式接口添加另一半什么'缺少

模板?替换它们?


只是在上下大声思考......


类抽奖

{

虚拟空白Draw()= 0;

};


类窗口

{

void Draw();

};


class Graphic

{

void Draw();

};


class BillyTheKid

{

void Draw( );

};


窗口,图形,BillyTheKid都是隐含的Drawables。

图形不是窗口但是因为Window不是一个

接口类(纯ABC)。当然,如果你将BillyTheKid传递给Canvas对象,你会得到奇怪的结果,但隐含接口的概念是什么?/ br
仍有待追求?


嗯....我说的是方面还挺?我们是否需要所有

这些机制:模板,模板概念,方面,

动态类型,隐式接口......其他?我想我是以b / b
想知道如果impicit接口解决了,或者可以解决其他人遇到的很多问题。


嗯,这对于圣诞夜来说足够头脑风暴(!)。


Tony

class Interface
{
public:
virtual void DoItNow()=0;
};

class A: public Interface
{
public:
void DoItNow(); // satisfies interface explicitly
};

class B
{
public:
void DoItNow(); // could satisfy interface implicitly
};

// Must wrap B to get a class to behave like Interface :(
//
class C: public Interface
{
B rep;
public:
void DoItNow(){ rep.DoItNow(); }
};

void some_func(Interface* obj)
{
obj->DoItNow();
}

void another_func()
{
A a; // IS explicitly an Interface
B b; // is NOT an explicit Interface
C c; // wraps a B to use it as an Interface
some_func(&a); // obviously OK
some_func(&b); // error! b is not derived from Interface
some_func(&c); // obviously OK
}

If the second call worked via implicit interfaces, the
wrapper class C could be eliminated. Am I describing
dynamic typing? Are Implicit Interfaces what template-
only "concepts" (proposed) are hinting at? I don''t think
it''s dynamic typing, becuase I''d want to know at compile
time whether an object satisfies an interface or not.
Templates consider type while interfaces consider behavior.
Do Implicit Interfaces add "the other half" of what''s missing
to templates? Replace them?

Just thinking out loud above and below...

class Drawable
{
virtual void Draw()=0;
};

class Window
{
void Draw();
};

class Graphic
{
void Draw();
};

class BillyTheKid
{
void Draw();
};

Window, Graphic, BillyTheKid are all Drawables implicitly.
A Graphic is not a Window though because Window is not an
interface class (pure ABC). Sure, you''ll get weird results if
you pass a BillyTheKid to a Canvas object, but is the concept
of Implicit Interfaces still something to be pursued?

Hmmm.... I''m talking about "aspects" kinda? Do we need all
these mechanisms: templates, template concepts, aspects,
dynamic typing, implicit interfaces... others? I guess I''m
wondering in a remote way if impicit interfaces solve or could
solve a lot of the problems the other ones do.

Well that''s enough brainstorming for Xmas night (!).

Tony

推荐答案

Tony写道:


< ...>


#include< iostream>

#ifdef __GXX_CONCEPTS__

//http://www.generic-programming.org/software/ConceptGCC/

auto concept Interface< typename T> {

void T :: DoItNow();

};


#endif

/ / class接口

// {

// public:

//虚拟无效DoItNow()= 0;

//

//

//};


A级//:公共接口

{

public:

void DoItNow(){std :: cout<< A DoItNow \ n;;} //满足

};


B级

{

public:

void DoItNow(){std :: cout<< B DoItNow \ n;}} //可以满足

};


//必须包装B以使类的行为类似于接口: (

//

C级//:公共接口

{

B rep;

public:

void DoItNow(){rep.DoItNow();}

};


template< ; typename T>

#ifdef __GXX_CONCEPTS__

其中接口< T>

#endif

void some_func(T * obj)

{

obj-> DoItNow();

}


无效another_func()

{

A a; //明确是一个接口

B b; //不是一个明确的接口

C c; //包裹B将其用作界面

some_func(& a); //显然可以

some_func(& b); //错误!b不是来自界面

some_func(& c); //显然可以

}


int main()

{

another_func();

}


// ------------------------

问候

Andy Little

Tony wrote:

<...>

#include <iostream>
#ifdef __GXX_CONCEPTS__
//http://www.generic-programming.org/software/ConceptGCC/
auto concept Interface<typename T>{
void T::DoItNow();
};

#endif
//class Interface
//{
// public:
// virtual void DoItNow()=0;
//
//
//};

class A //: public Interface
{
public:
void DoItNow(){std::cout << "A DoItNow\n";} // satisfies
};

class B
{
public:
void DoItNow(){std::cout << "B DoItNow\n";} // could satisfy
};

// Must wrap B to get a class to behave like Interface :(
//
class C //: public Interface
{
B rep;
public:
void DoItNow(){ rep.DoItNow(); }
};

template <typename T>
#ifdef __GXX_CONCEPTS__
where Interface<T>
#endif
void some_func(T* obj)
{
obj->DoItNow();
}

void another_func()
{
A a; // IS explicitly an Interface
B b; // is NOT an explicit Interface
C c; // wraps a B to use it as an Interface
some_func(&a); // obviously OK
some_func(&b); // error! b is not derived from Interface
some_func(&c); // obviously OK
}

int main()
{
another_func();
}

//------------------------
regards
Andy Little




" kwikius" < an ** @ servocomm.freeserve.co.ukwrote in message

news:11 ********************** @ 79g2000cws .googlegro ups.com ...

"kwikius" <an**@servocomm.freeserve.co.ukwrote in message
news:11**********************@79g2000cws.googlegro ups.com...

Tony写道:


< ...>


#include< iostream>

#ifdef __GXX_CONCEPTS__

//http://www.generic-programming.org/software/ConceptGCC /

auto concept接口< typename T> {

void T :: DoItNow();

};


#endif

//班级界面

// {

//公众:

//虚拟无效DoItNow()= 0;

//

//

//};


A级//:公共接口

{

public:

void DoItNow(){std :: cout<< ; A DoItNow \ n;;} //满足

};


B级

{

public:

void DoItNow(){std :: cout<< B DoItNow \ n;}} //可以满足

};


//必须包装B以使类的行为类似于接口: (

//

C级//:公共接口

{

B rep;

public:

void DoItNow(){rep.DoItNow();}

};


template< ; typename T>

#ifdef __GXX_CONCEPTS__

其中接口< T>

#endif

void some_func(T * obj)

{

obj-> DoItNow();

}


无效another_func()

{

A a; //明确是一个接口

B b; //不是一个明确的接口

C c; //包裹B将其用作界面

some_func(& a); //显然可以

some_func(& b); //错误!b不是来自界面

some_func(& c); //显然可以

}


int main()

{

another_func() ;

}
Tony wrote:

<...>

#include <iostream>
#ifdef __GXX_CONCEPTS__
//http://www.generic-programming.org/software/ConceptGCC/
auto concept Interface<typename T>{
void T::DoItNow();
};

#endif
//class Interface
//{
// public:
// virtual void DoItNow()=0;
//
//
//};

class A //: public Interface
{
public:
void DoItNow(){std::cout << "A DoItNow\n";} // satisfies
};

class B
{
public:
void DoItNow(){std::cout << "B DoItNow\n";} // could satisfy
};

// Must wrap B to get a class to behave like Interface :(
//
class C //: public Interface
{
B rep;
public:
void DoItNow(){ rep.DoItNow(); }
};

template <typename T>
#ifdef __GXX_CONCEPTS__
where Interface<T>
#endif
void some_func(T* obj)
{
obj->DoItNow();
}

void another_func()
{
A a; // IS explicitly an Interface
B b; // is NOT an explicit Interface
C c; // wraps a B to use it as an Interface
some_func(&a); // obviously OK
some_func(&b); // error! b is not derived from Interface
some_func(&c); // obviously OK
}

int main()
{
another_func();
}



这并没有回答现在提出的任何问题。


Tony

That doesn''t answer any of the questions posed now does it.

Tony




Tony写道:

Tony wrote:

>

这并没有回答现在提出的任何问题。
>
That doesn''t answer any of the questions posed now does it.



它解决了编码问题。


至于其他人......可能你最好的选择是精神病医生。


问候

Andy Little

It solves the coding problems.

As for the others... probably your best bet is a psychiatrist.

regards
Andy Little


这篇关于隐式接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆