please help me in distinguish redefining functions, overloading functions and overriding functions.

X

Xiangliang Meng

Hi, all.

When reading C++ books, I'm alway confused by those terms "redefining
functions", "overloading functions" and "overriding functions".

Please give me some comments on those terms. Thanks.

If giving more strategy hehind them, more helpful.

Best Regards,

Xiangliang Meng
 
V

Victor Bazarov

Xiangliang Meng said:
When reading C++ books, I'm alway confused by those terms "redefining
functions", "overloading functions" and "overriding functions".

Please give me some comments on those terms. Thanks.

If giving more strategy hehind them, more helpful.

Overloading is defining functions with the same name but with
different arguments in the _same_scope_. For example, members
of the same class would be overloaded if they have the same
name but different arguments (even just 'const' after the
function declaration counts):

struct foo {
void overloaded();
void overloaded(int); // same scope
};

Redefining (correct term is "hiding") is declaring a function
(and in fact, any symbol) with the same name in a more enclosed
scope. For the consistency's sake, derived classes' scope is
considered more enclosed than the base class' one.

struct base {
void somefunction();
};

struct derived : base {
void somefunction(int); // hides base::somefunction
};

Overriding relates to _virtual_ function _only_. And it can
happen if the function that overrides has the _same_ arguments:

struct base {
virtual void foo();
};

struct derived : base {
void foo(); // overrides base::foo
};

struct derived2 : base {
void foo(int); // does NOT override but instead _hides_
};

As to strategy, I am not sure what to tell you. Each of those
language features has its uses. Practice.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top