Solved

argument of a function

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 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