从聚合内部向上调用 [英] Calling upwards from within an aggregation

查看:73
本文介绍了从聚合内部向上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两节课:


班级酒吧

{

公开:

void RunTheBar(void);

};


class foo

{

private:

bar bar_;

public:

void Callback(void);

void RunTheBar(void){bar_。 RunTheBar();}

...

};


什么是设计bar的最佳方式;这样它可以在其RunTheBar()方法中调用

foo :: Callback()




这很漂亮直接使用Borland C ++ Builder扩展

__ closure:

typedef void(__closure * MyCallback)(void);


班级酒吧

{

私人:

MyCallback回调_;

公开:

bar(void):callback_(0){}

void RunTheBar(void);

void SetCallback(MyCallback Acallback){callback_ = Acallback ;}

};


空栏:: RunTheBar(无效)

{

/ / ...

if(callback_)

{

callback_();

}

// ...

}


class foo

{

private :

bar bar_;

public:

foo(void)

{

bar_.SetCallback(Callback);

}

void Callback(void);

void RunTheBar(void){bar_.RunTheBar() ; }

};


有没有办法可以在不使用非标准__closure的情况下完成此操作,

但仍然没有& ;栏"需要定义foo?如果bar,那将是很好的

可以包含除foo之外的对象。每次都不需要

来改变它。


-

Simon Elliott
http://www.ctsn.co.uk/

>

If I have two classes:

class bar
{
public:
void RunTheBar(void);
};

class foo
{
private:
bar bar_;
public:
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
...
};

What''s the best way to design "bar" such that it can call
foo::Callback()
from within its RunTheBar() method?

This is pretty straightforward using the Borland C++Builder extension
"__closure":

typedef void (__closure *MyCallback)(void);

class bar
{
private:
MyCallback callback_;
public:
bar(void):callback_(0){}
void RunTheBar(void);
void SetCallback(MyCallback Acallback){callback_ = Acallback;}
};

void bar::RunTheBar(void)
{
// ...
if (callback_)
{
callback_();
}
// ...
}

class foo
{
private:
bar bar_;
public:
foo(void)
{
bar_.SetCallback(Callback);
}
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
};

Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.

--
Simon Elliott
http://www.ctsn.co.uk/


推荐答案

Simon Elliott写道:
Simon Elliott wrote:
我有没有办法在不使用非标准__closure的情况下做到这一点,
但是仍然没有酒吧需要定义foo?如果bar,那将是很好的。可以包含除foo之外的对象。没有
每次都需要改变它。
Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be
nice if "bar" could be contained by objects other than "foo" without
needing to change it each time.




你看过boost :: function吗?


- -

Attila aka WW



Did you look at boost::function?

--
Attila aka WW


星期五,2003年9月26日11:39:47 +0100,Simon Elliott

< si *** @ nospam.demon.co.uk>写道:
On Fri, 26 Sep 2003 11:39:47 +0100, Simon Elliott
<si***@nospam.demon.co.uk> wrote:
如果我有两个班级:

班级栏目
公开:
void RunTheBar(void) ;
};

class foo
{
私人:
bar bar_;
公开:
void回调(无效) ;
void RunTheBar(void){bar_.RunTheBar();}
...
};

什么是最好的设计方法? BAR"这样它可以从RunTheBar()方法中调用
foo :: Callback()
使用Borland C ++ Builder扩展这是非常简单的>__ closure:

typedef void(__closure * MyCallback)(void);


嗯,我认为GCC也有这样的东西。

班级酒吧
私密:
MyCallback回调_ ;
public:
bar(void):callback_(0){}
void RunTheBar(void);
void SetCallback(MyCallback Acallback){callback_ = Acallback;}
};

空栏:: RunTheBar(无效)
{
// ...
if(callback_)
{
callback_();
}
// ...

class foo
{
私人:
吧bar_;
public:
foo(void)
{
bar_.SetCallback(Callback);
}
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
};

我有没有办法在不使用非标准__closure的情况下做到这一点,
但仍然没有"栏"需要定义foo?如果bar,那将是很好的
可以包含除foo之外的对象。每次都不需要更改它。
If I have two classes:

class bar
{
public:
void RunTheBar(void);
};

class foo
{
private:
bar bar_;
public:
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
...
};

What''s the best way to design "bar" such that it can call
foo::Callback()
from within its RunTheBar() method?

This is pretty straightforward using the Borland C++Builder extension
"__closure":

typedef void (__closure *MyCallback)(void);
Hmm, I think GCC has something like this too.

class bar
{
private:
MyCallback callback_;
public:
bar(void):callback_(0){}
void RunTheBar(void);
void SetCallback(MyCallback Acallback){callback_ = Acallback;}
};

void bar::RunTheBar(void)
{
// ...
if (callback_)
{
callback_();
}
// ...
}

class foo
{
private:
bar bar_;
public:
foo(void)
{
bar_.SetCallback(Callback);
}
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
};

Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.




如果你可以使用第三方库,那么boost.function就是你的价值。
想要:


#include< boost / function.hpp>

#include< boost / bind.hpp>

班级酒吧

{

public:

typedef boost :: function< void()> MyCallback;

bar(void){}

void RunTheBar(void);

void SetCallback(MyCallback Acallback){callback_ = Acallback;}

私人:

MyCallback回调_;

};


void bar :: RunTheBar(void )

{

// ...

if(!callback_.empty())

{

callback_();

}

// ...

}


class foo

{

私人:

bar bar_;

public:

foo(无效)

{

bar_.SetCallback(boost :: bind(& foo :: Callback,this));

}

void Callback(void);

void RunTheBar(void){bar_.RunTheBar();}

};


请参阅 www.boost.org 。 />

Tom



If you can use 3rd party libraries, then boost.function is what you
want:

#include <boost/function.hpp>
#include <boost/bind.hpp>

class bar
{
public:
typedef boost::function<void()> MyCallback;
bar(void){}
void RunTheBar(void);
void SetCallback(MyCallback Acallback){callback_ = Acallback;}
private:
MyCallback callback_;
};

void bar::RunTheBar(void)
{
// ...
if (!callback_.empty())
{
callback_();
}
// ...
}

class foo
{
private:
bar bar_;
public:
foo(void)
{
bar_.SetCallback(boost::bind(&foo::Callback, this));
}
void Callback(void);
void RunTheBar(void){bar_.RunTheBar();}
};

See www.boost.org.

Tom





Simon Elliott写道:


Simon Elliott wrote:

[snip]
我有没有办法可以做到这一点使用非标准__closure,
但仍然没有bar需要定义foo?如果bar,那将是很好的
可以包含除foo之外的对象。无需每次都更改它。
[snip]
Is there a way I can do this without using the non standard __closure,
but still without "bar" needing a definition of "foo"? It would be nice
if "bar" could be contained by objects other than "foo" without needing
to change it each time.




是的。

定义一个类,例如。 ''CallbackReceiver''

每个希望使用bar的课程都必须从该课程中获得
。有了多重继承,你可以为每个班级添加作为回调接收器的功能



bar :: RunThebar需要一个指向CallbeckReceiver

对象的指针,用于进行回调:


class CallbackReceiver

{

public:

void Callback()= 0;

};


class bar

{

public:

void RunTheBar(CallbackReceiver * pCalledFrom)

{

...

pCalledFrom-> Callback();

...

}

};


class foo:public CallbackReceiver

{

private:

bar bar_;


public:

void Callback();

void RunTheBar()

{

bar_.RunTheBar(this);

}

...

};


-

Karl Heinz Buchegger
kb ***** *@gascad.at


这篇关于从聚合内部向上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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