argument of a function

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

Question by:meow00
LVL 30

Accepted Solution

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 ?

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.

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.
LVL 10

Assisted Solution

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




Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 86
I could not build boost code, 10 96
Exception thrown at 0x00007FFD5BC81F28 7 50
Adding items to a C# list incrementally 5 36
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

820 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