473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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
4 5819
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
Thanks for your pretty answer. Now I got it. :)

Oct 14 '05 #3
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
50
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to...
6
by: Charles Sullivan | last post by:
I define and initialize an array of structures like the following, (where the <verbiage within angle bracketsis just meant to be explanatory): int func1(<argument prototypes>); int...
3
ADezii
by: ADezii | last post by:
Null as it relates to database development is one of life's little mysteries and a topic of total confusion for novices who venture out into the database world. A Null Value is not zero (0), a zero...
2
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.