Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Mar 19, 2001

WEBINAR:

Live

Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js


Date: 1/31/2018 @ 2 p.m. ET

Different Values For Default Arguments in Different Scopes


In the same scope, it’s not possible to have different values for default arguments. For example, this would be an error:
 
void foo(int i = 0){
    cout << i;
}
void foo(int = 1);//error

But default arguments can indeed take different values in different scopes. For example:
 
int Func(void){
    foo(int = 1);//ok, 1 will be used as the default value in the scope of
this function
}

To enforce this, a parameter must be supplied to the function. Declare the function without a default parameter:
 
int Func(void){
    foo(int);//disable the default parameter.
    foo(1);//ok.
    foo();//error, function foo() takes one parameter.
}

The opposite is also true--a function can be declared to take a default parameter even if it’s not defined that way.
 
void foo(int i){
	cout << i;
}

int Func(){
	void foo(int = 1);//enable default parameter
	foo();//ok.
}
Nitin Garg
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date