2

Non mumber function can be delcared multiple times while member function can only be declared once? Is this right ? My example seems saying yes.

But Why ?

class Base{
public:
    int foo(int i);
    //int foo(int i=10); //error C2535: 'void Base::foo(int)' : member function already defined or declared
};

//but it seems ok to declare it multiple times
int foo(int i);
int foo(int i=10);

int foo(int i)
{
    return i;
}

int main (void)
{
    int i = foo();//i is 10 
}
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
RoundPi
  • 5,819
  • 7
  • 49
  • 75

3 Answers3

6

From the Standard (2003), §8.3.6/4 says,

For non-template functions, default arguments can be added in later declarations of a function in the same scope.

Example from the Standard itself:

void f(int, int);
void f(int, int = 7);

The second declaration adds default value!

Also see §8.3.6/6.

And an interesting (and somewhat related) topic:

And §9.3/2,

Except for member function definitions that appear outside of a class definition, and except for explicit specializations of member functions of class templates and member function templates (14.7) appearing outside of the class definition, a member function shall not be redeclared.

Hope that helps.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Then isn't that perfect, @James? The quoted part of the standard says it applies to non-template functions. – Rob Kennedy Sep 22 '11 at 17:37
  • @UncleBens: Thanks. That quote is very precise. I added it to my answer. And yeah, after re-reading the question, I felt that the answer seems incomplete and I was looking for another quote from the spec. You did it before me. – Nawaz Sep 22 '11 at 17:55
  • This seems odd to me, is it the linker that adds default arguments in these cases? – Captain Giraffe Sep 22 '11 at 18:24
  • 1
    @CaptainGiraffe: No. It's compiler itself. See this also : http://stackoverflow.com/questions/5637679/default-argument-in-the-middle-of-parameter-list – Nawaz Sep 22 '11 at 18:27
  • @Nawaz There seems to be a theme going, me questioning/adding a side-effect from one of your statements + Johannes - litb having stated the question + your answer before :) I find this quite amusing =) Anyway you have my utmost respect for your knowledge and patience. Thanks. – Captain Giraffe Sep 22 '11 at 19:17
  • @CaptainGiraffe: Honestly, I didn't understand your comment :| – Nawaz Sep 22 '11 at 19:21
  • The one about the linker or the last one about -litb being ubiquitous to my comments/your answers? Not important. Just thanks. – Captain Giraffe Sep 22 '11 at 19:29
  • @CaptainGiraffe: Of course, the last one.. below my comment in which I gave you a link to Johannes's question and my answer. – Nawaz Sep 22 '11 at 19:31
  • @Nawas I'm trying hard here to just say thanks for your efforts ok! – Captain Giraffe Sep 22 '11 at 19:52
1

You get the same result with this simplified version:

int foo() ;
int foo() ; // OK -- extern functions may be declared more than once
class C {
  int foo() ;
  int foo() ; // Error -- member function may not be declared more than once
} ;

Perhaps the reason is historical -- lots of C code used redeclaration of extern functions, so they had to be allowed.

TonyK
  • 16,761
  • 4
  • 37
  • 72
0

It certainity works - but I think it is bad practice.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127