Solved

argument of a function

Posted on 2003-11-12
2
213 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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