代表团问题...... [英] Delegation question...

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

问题描述

C ++中用于委派

函数调用的常用方法/设计模式(如果有)是什么,这些函数调用不是由某个类处理的。公共

继承是单向的,但并非所有类都要从(例如STL)继承



示例:


class A

{

public:

foo();


私人:

设置< stringmyset;

}


一个myObj;

myObj 。插入(); //当然是编译器错误

是否存在一些机制(直接或间接),其中myObj未处理的函数是否被委托给另一个对象(例如myset)?

解决方案

barcaroller写道:


常见的方式/设计模式是什么(如果有的话,在C ++中用于委托

函数调用,这些调用不是由某个类处理的。公共

继承是单向的,但并非所有类都要从(例如STL)继承




示例:


A级

{

公开:

foo();


私人:

设置< stringmyset;

}


一个myObj;

myObj.insert(); //当然是编译器错误


是否存在一些机制(直接或间接),其中myObj未处理的函数被委托给另一个对象(例如myset)?



我相信你可以在课堂上添加以下内容:

set< string& operator->(){return myset; }


然后你可以做myObj-> insert()

虽然这看起来有点麻烦。


-

Daniel Pitts的'科技博客:< http://virtualinfinity.net/wordpress/>


barcaroller写道:


C ++中用于委派的常用方法/设计模式(如果有的话)

函数调用不是由某一类。公共

继承是单向的,但并非所有类都要从(例如STL)继承




示例:


A级

{

公开:

foo();


私人:

设置< stringmyset;

}


一个myObj;

myObj.insert(); //当然是编译器错误


是否存在一些机制(直接或间接),其中myObj未处理的函数被委托给另一个对象(例如myset)?



不,C ++不支持这种授权形式。


-

Ian柯林斯。


24 mai,00:43,Ian Collins< ian-n ... @ hotmail.comwrote:


barcaroller写道:


C ++中用于委派的常用方法/设计模式(如果有)

函数调用不是由某个类处理的。公共

继承是单向的,但并非所有类都要从(例如STL)继承



示例:


class A

{

public:

foo();

私人:

set< stringmyset;

}


myObj;

myObj.insert(); //编译错误当然是


是否存在某种机制(直接或间接)那是不是由myObj处理的
被委托给另一个对象(例如myset)?


否,C ++不支持这种形式的委托。



不直接。我认为,你最接近的是使用

私有继承和使用声明。


请注意,这种类型的委托是有效暴露部分的/>
你的内部,在某种程度上。虽然很明显,但我更倾向于明确转发,所以

对象的完整界面是不可用的。

大部分时间,至少;我还至少有一个案例,该对象的

非可变接口正好是

std :: vector< std :: string我可以设想其他人。哪个

意味着我必须复制很多(包括像

typedef'这样的东西)。但它也不是100%重复;我有

iterator typedefed到std :: vector< std :: string> :: const_iterator,

例如。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33(0)1 30 23 00 34


What is the common way/design-pattern (if any) in C++ for delegating
function calls that are not handled by a certain class. Public
inheritance would be one way but not all classes are meant to inherit
from (e.g. STL).
Example:

class A
{
public:
foo();

private:
set<stringmyset;
}

A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?

解决方案

barcaroller wrote:

What is the common way/design-pattern (if any) in C++ for delegating
function calls that are not handled by a certain class. Public
inheritance would be one way but not all classes are meant to inherit
from (e.g. STL).
Example:

class A
{
public:
foo();

private:
set<stringmyset;
}

A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?

I believe you can add the following to your class:
set<string&operator->() { return myset; }

and then you can do myObj->insert()
Although, this seems a bit troublesome.

--
Daniel Pitts'' Tech Blog: <http://virtualinfinity.net/wordpress/>


barcaroller wrote:

What is the common way/design-pattern (if any) in C++ for delegating
function calls that are not handled by a certain class. Public
inheritance would be one way but not all classes are meant to inherit
from (e.g. STL).
Example:

class A
{
public:
foo();

private:
set<stringmyset;
}

A myObj;
myObj.insert(); // compiler error of course
Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?

No, C++ does not support this form of delegation.

--
Ian Collins.


On 24 mai, 00:43, Ian Collins <ian-n...@hotmail.comwrote:

barcaroller wrote:

What is the common way/design-pattern (if any) in C++ for delegating
function calls that are not handled by a certain class. Public
inheritance would be one way but not all classes are meant to inherit
from (e.g. STL).

Example:

class A
{
public:
foo();
private:
set<stringmyset;
}

A myObj;
myObj.insert(); // compiler error of course

Is there some mechanism (direct or indirect) where a function that is
not handled by myObj gets delegated to another object (e.g. myset)?

No, C++ does not support this form of delegation.

Not directly. The closest you can come, I think, is to use
private inheritance and using declarations.

Note that this type of delegation is effectively exposing part
of your internals, to some degree. Although significantly
wordier, I rather favor being explicit in forwarding, so that
the complete interface of the object isn''t available. Most of
the time, at least; I also have at least one case where the
non-mutable interface of the object is exactly that of
std::vector< std::string and I can conceive of others. Which
means that I do have to duplicate a lot (including things like
typedef''s). But it''s not 100% duplication either; I have
iterator typedefed to std::vector<std::string>::const_iterator,
for example.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于代表团问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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