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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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