By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,313 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,313 IT Pros & Developers. It's quick & easy.

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

P: n/a
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::ostream& print( int , std::ostream& 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::ostream& print( int , std::ostream& 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() ;

}

Oct 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
aling wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 14 '05 #2

P: n/a
Thanks for your pretty answer. Now I got it. :)

Oct 14 '05 #3

P: n/a
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!

Oct 14 '05 #4

P: n/a
In article <11**********************@o13g2000cwo.googlegroups .com>,
aling <li*********@163.com> wrote:
I thought that the default argument of function must be a constant value.


It need not be.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.