Solved

argument of a function

Posted on 2003-11-12
2
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.

752 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