方法重载和继承 [英] Method overloading and inheritance

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

问题描述

大家好,


根据下面的代码,有人能告诉我:

a)这是正常行为吗?

b如果是的话,背后的原因是什么?我希望

A :: method(int)可以继承B.


编译器:gcc 4.1,Linux

谢谢,

Mihai


---------------------- -


#include< stdio.h>


A级{

公开:

虚拟空方法(int);

};


B级:公共A {

public:

虚拟空方法();

};


void A :: method(int n){

fprintf(stderr,A :: method(%d)\ n,n);

}


void B :: method(){

fprintf(stderr," B :: method\ n");

}


int main(int argc,char ** argv){


B b;

b.method(3);

return 0;

}

-----------------


迈克@克星:〜/ work / inh_test $ g ++ -o main main.cpp

main.cpp:在函数''int main(int,char **)'':

main.cpp:31:错误:没有垫子ching函数用于调用''B :: method(int)''

main.cpp:18:注意:候选者是:virtual void B :: method()

Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

void A::method(int n){
fprintf(stderr, "A::method(%d)\n", n);
}

void B::method(){
fprintf(stderr, "B::method\n");
}

int main(int argc, char** argv){

B b;
b.method(3);
return 0;
}
-----------------

mike@nemesis:~/work/inh_test$ g++ -o main main.cpp
main.cpp: In function ''int main(int, char**)'':
main.cpp:31: error: no matching function for call to ''B::method(int)''
main.cpp:18: note: candidates are: virtual void B::method()

推荐答案

g ++ -o main main.cpp

main.cpp:在函数''int main(int,char **)'': br />
main.cpp:31:错误:没有匹配函数来调用''B :: method(int)''

main.cpp:18:注意:候选者是:virtual void B :: method()
g++ -o main main.cpp
main.cpp: In function ''int main(int, char**)'':
main.cpp:31: error: no matching function for call to ''B::method(int)''
main.cpp:18: note: candidates are: virtual void B::method()


Mihai Osian写道:
Mihai Osian wrote:

大家好,


鉴于下面的代码,有谁能告诉我:

a)这是正常行为吗?

b)如果是,那是什么原因背后呢?我希望

A :: method(int)可以继承B.


编译器:gcc 4.1,Linux

谢谢,

Mihai


---------------------- -


#include< stdio.h>


A级{

公开:

虚拟空方法(int);

};


B级:公共A {

public:

虚拟空方法();

};
Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};



了解名称隐藏在常见问题解答或谷歌上。


V

-

请在回复时删除资金''A'电子邮件

我没有回复最热门的回复,请不要问

Read about "name hiding" in the FAQ or on Google.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Mihai Osian写道:
Mihai Osian wrote:

大家好,


鉴于下面的代码,有谁能告诉我:

a)这是正常行为吗?

b)如果是,那背后的原因是什么?我希望

A :: method(int)可以继承B.


编译器:gcc 4.1,Linux

谢谢,

Mihai


---------------------- -


#include< stdio.h>


A级{

公开:

虚拟空方法(int);

};


B级:公共A {

public:

虚拟空方法();

};


void A :: method(int n){

fprintf(stderr,A :: method(%d)\ n,n);

}


void B :: method(){

fprintf(stderr," B :: method\ n");

}


int main(int argc,char ** argv){


B b;

b.method(3);

return 0;

}


-----------------


mike @ nemesis:〜/ work / inh_test
Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

void A::method(int n){
fprintf(stderr, "A::method(%d)\n", n);
}

void B::method(){
fprintf(stderr, "B::method\n");
}

int main(int argc, char** argv){

B b;
b.method(3);
return 0;
}
-----------------

mike@nemesis:~/work/inh_test


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

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