Solved

argument of a function

Posted on 2003-11-12
2
209 Views
Last Modified: 2010-04-01
To C++ experts,

 I have a code as follows :
--------------------------------------------------------------
class A{} ;
class B
{   public :
      void f(A const& a){ cout << "f" << endl ;}
      void g(A const&){ cout << "g" << endl ;}
};

int main()
{
  A a1 ;
  B b1 ;
  b1.f(a1) ;
  b1.g(a1) ;
  return 0 ;
}
---------------------------------------------
  It seems that both f(A const& a) and g(A const&) work. However, I am wondering is there any difference between them ? which one is better and which one should I use in general ?

  Thanks a lot !

meow.
 
0
Comment
Question by:meow00
2 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 30 total points
ID: 9737659
>>It seems that both f(A const& a) and g(A const&) work. However, I am wondering is there any difference between them ? which one is better and which one should I use in general ?

Yes.
Since function g doesn't have the argument name, the argument is not available for use within the function.

This type of declaration is commonly used to avoid compiler warnings or errors.
Example

int foo(int xyz)
{
  return 3;
}

Some compilers will give a warning for the above function.
*** Waring variable xyz declared but not used *****

To avoid such a warning, you could declare the function without the argument name.

int foo(int)
{
  return 3;
}

Now the compiler will not give a warning for unused variable.

Of course, if you don't need the variable, then you shouldn't have it in the function declaration.
However, this method is usefull when a function argument is no longer needed but you dont' wish to change all the code that is calling the function.
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 20 total points
ID: 9741117
Just to add, the above technique is termed as having Place-Holder Arguments

It is used when u know want to keep some dummy parameter to a function, so that u could use it later without affecting the client code using that function

HTH

Amit

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now