指向继承类的成员函数的指针到成员函数 [英] Pointer-to-member-function pointing to a member function of an inherited class

查看:81
本文介绍了指向继承类的成员函数的指针到成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题基本上说明了我遇到的问题。对于一个

的例子,假设我有一个具有函数f的A类。然后我还要

有一个继承自A的B类并且有一个函数g。


A级

{

public:void f(int,float);

};


class B:public A

{

public:void g(int,float);

};

好​​的,现在有了所有的设置,如果我创建了一个指向成员的指针

函数如下:

typedef void(A :: * MFP)(int a,float b);


如果我然后使用它如


MFP mfp =& A :: f;


那么一切正常。


即使我这样做:


MFP mfp2 =& B :: f;

然后一切仍然有效(大概是因为f没有定义在B

,然后必须来自A)。但是,如果我试试这个:


MFP mfp3 =& B :: g;


然后它抱怨B :: g是不是正确的类型,因为它不是A类的

成员函数.protype签名是相同的,无论是
(int,float),都没办法使这项工作?在我看来它应该有效,因为任何B的实例仍然是定义为A的任何一个实例。任何人做过这样的事情吗?


-akiri是

The subject basically says what I am having trouble with. For an
example lets say I have a class A which has a function f. I then also
have a class B which inherits from A and has a function g.

class A
{
public: void f(int,float);
};

class B : public A
{
public: void g(int, float);
};
Okay, now with all that setup, if I created a pointer to a member
function something like this:

typedef void (A::*MFP)(int a, float b);

if I then use it such as

MFP mfp = &A::f;

then everything works fine.

even if I do:

MFP mfp2 = &B::f;

then everything still works (presumably because f is not defined in B
and must then be from A). However, if I try this:

MFP mfp3 = &B::g;

then it complains that B::g is not of the right type since it is not a
member function of class A. The protype signature is the same, both
(int, float), is there no way to make this work? It seemed to me that
it should have worked since any instance of B is still by definition an
instance of A. Anyone done anything like this?

-akiriwas

推荐答案

ak ****** @ gmail.com 写道:
该主题基本上说明了我遇到的问题。对于一个
例子,假设我有一个具有函数f的A类。然后我还有一个继承自A并具有函数g的B类。

A类
{
public:void f(int,float);
};

B组:公开A
{
公开:void g(int,float);
};

好的,现在有了所有的设置,如果我创建了一个指向成员的指针
这样的函数:

typedef void(A :: * MFP)(int a,float b );


尝试这样做:


typedef void(B :: * MFP)(int a,float b);
<如果我然后使用它如

MFP mfp =& A :: f;

那么一切正常。

如果我这样做:

MFP mfp2 =& B :: f;


& B :: f在编译完成后变为& A :: f,因为f是A的成员a / $
- 这是标准指定的一些魔法只有

与成员的文字指针一起使用。


A :: *成员指针可以转换为B :: *会员指针,但不是

另一种方式。 ie


typedef void(A :: * AMFP)(int a,float b);

typedef void(B :: * BMFP)(int a ,浮动b);

void f(AMFP a,AMFP b)

{

b = a; //合法

a = b; //非法

}

然后一切仍然有效(大概是因为f未在B中定义,必须来自A)。但是,如果我试试这个:

MFP mfp3 =& B :: g;


想一想 - 类型A的对象不能真正支持B类b的方法,因为它不可能真的是哪种类型对象你真的

有。

然后它抱怨B :: g不是正确的类型,因为它不是A类的成员函数原型签名是一样的,无论是(int,float),都没办法让这个工作吗?在我看来它应该有效,因为B的任何实例仍然是定义为A的实例。任何人做过这样的事情吗?
The subject basically says what I am having trouble with. For an
example lets say I have a class A which has a function f. I then also
have a class B which inherits from A and has a function g.

class A
{
public: void f(int,float);
};

class B : public A
{
public: void g(int, float);
};
Okay, now with all that setup, if I created a pointer to a member
function something like this:

typedef void (A::*MFP)(int a, float b);
Try making this :

typedef void (B::*MFP)(int a, float b);

if I then use it such as

MFP mfp = &A::f;

then everything works fine.

even if I do:

MFP mfp2 = &B::f;
&B::f becomes &A::f when the compiler is done with it because f is a
member of A - this is some magic specified by the standard and only
works with literal pointer to members.

An A::* member pointer can be converted to a B::* member pointer but not
the other way. i.e.

typedef void (A::*AMFP)(int a, float b);
typedef void (B::*BMFP)(int a, float b);
void f( AMFP a, AMFP b )
{
b = a; // legal
a = b; // illegal
}

then everything still works (presumably because f is not defined in B
and must then be from A). However, if I try this:

MFP mfp3 = &B::g;
Think about it - an object of type A can''t really support a method of
class B since it''s impossible to really which type of object you really
have.

then it complains that B::g is not of the right type since it is not a
member function of class A. The protype signature is the same, both
(int, float), is there no way to make this work? It seemed to me that
it should have worked since any instance of B is still by definition an
instance of A. Anyone done anything like this?




是。

这种情况的
选项是:


a)使用虚拟方法

b)使用函数(不是会员功能)



Yes.

options for this case are:

a) use virtual methods
b) use functions (not member functions)




< ak ****** @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...

<ak******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
该主题基本上说明了我遇到的问题。对于一个
例子,假设我有一个具有函数f的A类。然后我还有一个继承自A并具有函数g的B类。

A类
{
public:void f(int,float);
};

B组:公开A
{
公开:void g(int,float);
};

好的,现在有了所有的设置,如果我创建了一个指向成员的指针
这样的函数:

typedef void(A :: * MFP)(int a,float b );

如果我再使用它,比如

MFP mfp =& A :: f;

那么一切正常。
MFP mfp2 =& B :: f;

然后一切仍然有效(大概是因为f未在B中定义)
然后必须来自A)。但是,如果我试试这个:

MFP mfp3 =& B :: g;

然后它抱怨B :: g不是正确的类型,因为它是不是A级的成员函数。


正确。

原型签名是相同的,


与什么相同?你似乎认为''B''对象有多个名为''g'的函数
。只有一个,

''A''的成员(这是''B''的成员。)

both
(int,浮动),有没有办法使这项工作?在我看来它应该有效,因为B的任何实例仍然按照定义为A.


但是''g''是仍然只是A的成员,它不是''B'的成员。

当你从''A''派生''B''时,'' ''被'包含*'被''B''包含。

不是成员的重复。一个''B''对象将有两个

''部分'':'A''部分,''部分''包含任何成员

仅在''B'中定义(如果有的话)。


你想做什么,你认为你不能使用

& A :: g ??

有人做过这样的事吗?
The subject basically says what I am having trouble with. For an
example lets say I have a class A which has a function f. I then also
have a class B which inherits from A and has a function g.

class A
{
public: void f(int,float);
};

class B : public A
{
public: void g(int, float);
};
Okay, now with all that setup, if I created a pointer to a member
function something like this:

typedef void (A::*MFP)(int a, float b);

if I then use it such as

MFP mfp = &A::f;

then everything works fine.

even if I do:

MFP mfp2 = &B::f;

then everything still works (presumably because f is not defined in B
and must then be from A). However, if I try this:

MFP mfp3 = &B::g;

then it complains that B::g is not of the right type since it is not a
member function of class A.
Correct.
The protype signature is the same,
Same as what? You seem to think that a ''B'' object has
more than one function named ''g''. There is only one,
the member of ''A'' (which is a member of ''B'') .
both
(int, float), is there no way to make this work? It seemed to me that
it should have worked since any instance of B is still by definition an
instance of A.
But ''g'' is still only a member of ''A'', it''s not a member of ''B''.
When you derive ''B'' from ''A'', ''A'' is *contained* by ''B''. There
is no ''duplication'' of members. A ''B'' object will have two
''parts'': the ''A'' part, and the ''part'' containing any members
defined only in ''B'' (if any).

What are you trying to do that you think you can''t using
&A::g ??
Anyone done anything like this?




不,因为没有必要。具体到底是什么?b $ b试图做什么?


-Mike



No, because there''s no need. What specifically are you
trying to do?

-Mike


看这样用不同的名字:

1)一个人有一个名字

2)学生有一个名字

3)但不是每个人都有人有成绩。


班级人员

{

公开:

void name(int ,漂浮);

};

class学生:公共人物

{

public:

void grade(int,float);

};


int main(){


typedef void(Person :: * MFP)(int a,float b);


MFP mfp1 =& Person :: name;

MFP mfp2 =& Student :: name;

MFP mfp3 =& Student :: grade;

return 0;

}

Look at this way with different names:
1) a Person has a name
2) a Student has a name
3) but not every Person has a grade.

class Person
{
public:
void name(int,float);
};
class Student : public Person
{
public:
void grade(int, float);
};

int main() {

typedef void (Person::*MFP)(int a, float b);

MFP mfp1 = &Person::name;
MFP mfp2 = &Student::name;
MFP mfp3 = &Student::grade;
return 0;
}


这篇关于指向继承类的成员函数的指针到成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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