What's the rule of default argument of function in C++?

A

aling

What's the rule of default argument of function in C++? I found that
the default argument of function could not necessary be a constant
value. Is it true? Previously I thought that the default argument of
function must be a constant value.

Here is my sample code, it's compiled successully in VC7.1.

#include<iostream>
class Base{
public :
virtual Base* copy( Base* ){
std::cout << "Base::copy\n" ;
return 0;
}
virtual std::eek:stream& print( int , std::eek:stream& os = std::cout ){
os << "Base::print\n" ;
return os;
}
};
class Derived : public Base{
public :
Derived* copy( Base* ){
std::cout << "Derived::copy\n" ;
return 0;
}
std::eek:stream& print( int , std::eek:stream& os){
std::cout << "Derived::print\n" ;
return os;
}
};
int main(){
Base* p = new Derived ;
p->copy( p ) ;
p->print( 1 ) ;
p->print( 1 , std::cout ) ;
std::cin.get() ;

}
 
K

Karl Heinz Buchegger

aling said:
What's the rule of default argument of function in C++? I found that
the default argument of function could not necessary be a constant
value. Is it true? Previously I thought that the default argument of
function must be a constant value.

No.
The standard tells us that the default argument is simply an expression.
There are some things that are no allowed (using local variables, using
the keyword 'this') but pretty much anything else can serve as a default
argument. Look at this:

#include <iostream>

int foo()
{
std::cout << "foo called\n";
return 5;
}

void bar( int i = foo() )
{
std::cout << "bar called with " << i << "\n";
}

int main()
{
bar( 1 );
bar();
}

Just think of how you would handle default arguments, if you were a compiler.
I would do it like this:
When parsing, I would store to a function all arguments. With each argument
I would also store a string containing the default argument specification.
When the time has come to actually call that function I would look up all
the parameters, if they are specified in the call. If they are not, I would
simply substitute the parameters from the stored default arguments. This
gives a complete call specification which can then be compiled just as if
no default arguments were used.

so in
int main()
{
bar();
}

I recognize, that bar() wants 1 argument, but there is none given. Thus I
further look up the stored function signature and recognize the default
argument, which gets substituted in the source code text:

int main()
{
bar( foo() );
}

Now, that compiles perfectly.
 
P

Peter

A default argument was provided to improve flexibility of general
function especially constructor of object to handle the simple case.
Just for this convenience! the arguments which used by some passing
rules are still the same as others, so there are no different rules!
 

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,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top