功能对象继承 [英] function object inheritence

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

问题描述

我有一个函数对象,我从中导出另一个函数对象

,其中我覆盖了operator()。但是现在

基类中的operator()似乎无法访问。例如,以下代码没有
编译。关于如何使其工作的任何见解?


class C {};

struct X

{

void operator()(C const&){}

};


struct Y:public X

{

void operator()(float){}

} foo;


int main()

{

C c;

foo(c);

}

解决方案



" Malay Haldar" <莫** @ mailcity.com>在消息中写道

新闻:5c ************************* @ posting.google.co m ...


在派生类中声明一个名称会在基类中隐藏相同的名称

,除非你专门提出它。

struct Y:public X
{
使用X :: operator(); void operator()(float){}
} foo;



" Ron Natalie" < ro*@sensor.com>在消息中写道

新闻:3f ********************* @ news.newshosting.com。 ..


Malay Haldar <莫** @ mailcity.com>在消息中写道
新闻:5c ************************* @ posting.google.co m ...

在派生类中声明一个名称在基类中隐藏相同的名称
除非你专门提出它。

struct Y:public X
使用X :: operator();

void operator()(float){}
} foo;





嗯,请原谅我,但是我对同样的问题感兴趣并且

有几个问题:


1)使用没有命名空间支持的编译器的方法是什么?


2)是Y :: operator()( float)真的是X :: operator()的重载(C const&);

参数不同?


问候

Tim


" Tim" < no ****************** @ nospamhotmailnospam.nospamco mnospam>在消息新闻中写了

:br ********** @ news.cybercity.dk

" Ron Natalie" < ro*@sensor.com>在消息中写道
新闻:3f ********************* @ news.newshosting.com。 ..


Malay Haldar <莫** @ mailcity.com>在消息中写道
新闻:5c ************************* @ posting.google.co m ...

在派生类中声明一个名称会在基础
类中隐藏相同的名称,除非您明确提出它。

struct Y:public X
{using X :: operator();

void operator()(float){}
} foo;



<嗯,请原谅我




和我。

1)使用a做什么的方法是什么编译器没有命名空间
支持?


struct Y:public X

{

void operator()(float){}

void operator()(C const& arg)

{

X :: operator()(arg);

}

} foo;


2)Y :: operator()(float)真的是X :: operator()的重载(C
const) &安培);参数不同?




你的意思是真正重新定义。参数不同的事实

是无关紧要的。如果运算符名称相同,那么基类中

同名的所有运算符都是隐藏的(函数也是如此;例如,

例如,你在基类中有foo(int),然后在

派生类中的foo(char)将隐藏基类中的foo(int)。

-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发布)


I have a function object, from which I derive another function object
in which I overwrite the operator(). But now the operator() in the
base class seems inaccessible. For example, the following code doesn''t
compile. Any insights on how to make it work ?

class C{};
struct X
{
void operator()( C const & ){}
};

struct Y : public X
{
void operator()( float ){}
}foo;

int main()
{
C c;
foo(c);
}

解决方案


"Malay Haldar" <mo**@mailcity.com> wrote in message
news:5c*************************@posting.google.co m...

Declaring a name in a derived class hides the same name in the base class
unless you specifically bring it forward.

struct Y : public X
{ using X::operator(); void operator()( float ){}
}foo;



"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..


"Malay Haldar" <mo**@mailcity.com> wrote in message
news:5c*************************@posting.google.co m...

Declaring a name in a derived class hides the same name in the base class
unless you specifically bring it forward.

struct Y : public X
{


using X::operator();

void operator()( float ){}
}foo;




Hmm, exusing me for butting in, but I''m interested in the same issue and
have a couple of questions:

1) What''s the way to do this using a compiler without namespace support?

2) Is Y::operator()(float) really an overload of X::operator()(C const &);
the parameters are different?

Regards
Tim


"Tim" <no******************@nospamhotmailnospam.nospamco mnospam> wrote
in message news:br**********@news.cybercity.dk

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..


"Malay Haldar" <mo**@mailcity.com> wrote in message
news:5c*************************@posting.google.co m...

Declaring a name in a derived class hides the same name in the base
class unless you specifically bring it forward.

struct Y : public X
{ using X::operator();

void operator()( float ){}
}foo;




Hmm, exusing me for butting in



And me.
1) What''s the way to do this using a compiler without namespace
support?
struct Y : public X
{
void operator()( float ){}
void operator()( C const & arg)
{
X::operator ()(arg);
}
}foo;

2) Is Y::operator()(float) really an overload of X::operator()(C
const &); the parameters are different?



You mean "really a redefinition". The fact that the parameters are different
is irrelevant. If the operator name is the same, then all operators of the
same name in the base class are hidden (the same is true of functions; if,
for example, you have foo(int) in the base class, then foo(char) in the
derived class will hide foo(int) in the base class).
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


这篇关于功能对象继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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