超载/覆盖? [英] Overloading/Overriding ?

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

问题描述

大家好,


我是C ++的新手(我有一些C背景)。我已经读过这个话题了几次,但它似乎并没有下沉。


1.差异是什么重载和覆盖之间的关系?

2.什么时候比另一个更好?

3.虚拟功能如何与此主题相关

(重载/覆盖) - 使用虚拟

函数的真实世界示例将非常感谢。


谢谢

解决方案

> 1.重载和覆盖之间的区别是什么?


重载是几个具有相同名称的函数的定义

但不同的参数和/或不同的数字参数。


void Foo(int);

void Foo(int,int);

void Foo(char) ;


重写是为基类中定义的

函数编写一个不同的主体(在派生类中)。


班级基础

{

公开:

int Foo()

{

返回1;

}

};


派生类:公共基础

{

public:

int Foo()

{

返回2;

}

};

2.何时优先使用而不是另一种?
它们不是独家但是免费的。在基类中有多个重载函数可以在

派生类中重写。另外,当一个类来自

另一个类,其中重载可以在类中使用(或者对于非

3.如何与此主题相关的虚拟功能



我建议您阅读常见问题解答 http://www.parashift.com/c++-faq-lite/index.html


ve ********* @ hotmail.com 写道:

1.重载和覆盖之间的区别是什么?



重载是具有相同名称的几个函数的定义
但不同的参数和/或不同数量的参数。

void Foo(int);
void Foo(int,int) ;
void Foo(char);




只需添加,

重载c不使用类就可以完成。但是如果你使用一个类,那么这些函数必须在同一个类中。


此外,返回类型不相关。 Soo,如上所述只有

函数名称应该相同,而参数类型/数字必须是

不同。

< br>

ve*********@hotmail.com 写道:< blockquote class =post_quotes>

1。重载和覆盖之间的区别是什么?

重载是几个具有相同名称的函数的定义,但不同的参数和/或不同数量的参数。
void Foo(int);
void Foo(int,int);
void Foo(char);




重载_only_适用于名称*相同范围内的函数*。

重写是为基类中定义的
函数编写不同的主体(在派生类中)。


....和* only *如果基类中的函数声明为_virtual_

且* * *如果派生类中的函数具有同样的签名。

班级基地
{
公开:
int Foo()
{
返回1;
}
};

类派生:公共基地
公开:
int Foo()
{
返回2;
}
};


这里有一个名字隐藏的例子,因为'base >> Foo''不是虚拟的。

2。何时优先使用而不是另一种?



它们不是独家的,而是免费的。在基类中有多个重载函数可以在
派生类中重写。




这是不正确的。派生类中的所有函数_hide_基础

类的函数具有相同的名称,除了虚函数之外,

使用_same_signature_覆盖基类函数。


当一个类派生自另一个可以在类中使用重载的类(或非类函数定义)时,覆盖也只有作用。




重载只涉及同一个_scope_中的函数,即

同一个类或任何类之外。
< blockquote class =post_quotes>

3。如何与此主题相关的虚拟功能


我建议您阅读常见问题解答 http://www.parashift.com/c++-faq-lite/index.html




这总是很好的想法(tm)。


V


Hello everybody,

I''m new to C++ (I have some C background). I''ve read up on this topic a
few times but it just dosen''t seem to be sinking in.

1. Whats the difference between overloading and overriding?
2. When is one preferable to use as opposed to the other?
3. How are virtual functions related to this topic
(overloading/overriding) - a real world example of using virtual
functions would be very much appreciated.

Thank you

解决方案

> 1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);

overriding is writing a different body (in a derived class) for a
function defined in a base class.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};

2. When is one preferable to use as opposed to the other? They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class. Also overriding only works when a class is derived from
another class where overloading can be used inside a class (or for non
class function definitions).
3. How are virtual functions related to this topic


I''d suggest to read the FAQ at
http://www.parashift.com/c++-faq-lite/index.html


ve*********@hotmail.com wrote:

1. Whats the difference between overloading and overriding?



overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);



Just to add,
Overloading can be done without usage of classes. However if you are
using a class then these functions have to be in the same class.

Also, the return type is not relevant. Soo, as said above only the
function name should be same while paramter type/number must be
differnt.


ve*********@hotmail.com wrote:

1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);



Overloading _only_ applies to names of functions in the *same scope*.
overriding is writing a different body (in a derived class) for a
function defined in a base class.
.... and *only* if the function in the base class is declared _virtual_
and *only* if the function in the derived class has the same signature.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};
Here you have an example of name hiding since ''base::Foo'' isn''t virtual.

2. When is one preferable to use as opposed to the other?



They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class.



This is incorrect. All functions in the derived class _hide_ the base
class functions with the same name, except in the case of virtual ones,
which override the base class functions with the _same_signature_.

Also overriding only works when a class is derived from
another class where overloading can be used inside a class (or for non
class function definitions).



Overloading only relates to the functions in the same _scope_, i.e. in
the same class or outside of any class.

3. How are virtual functions related to this topic



I''d suggest to read the FAQ at
http://www.parashift.com/c++-faq-lite/index.html



It''s always a GOOD IDEA(tm).

V


这篇关于超载/覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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