0

Now, I'm not saying that I think that this would be anywhere near proper coding practice (assuming it's even possible); this question arises from a 2am mistake: Assume class definition resembling the following:

class myClass
{
public:
    void myMethod(const int & name);
}

and definition:

void myClass::myMethod(const int & altName)
{
    //manipulate altName
}

Note that parameter of both declaration and definition are of type const int &, the only difference is the variable name. I know that this does not (in Visual Studio 2012) throw a compiler error or warning, but could it cause an error while running the program?

Thanks!

Supremum
  • 542
  • 7
  • 23
ronman38
  • 11
  • 4
  • 1
    You have to distinguish (member) _variaböes_ and function _parameters_. The latter don't need to have the same names in declaration and definition. – πάντα ῥεῖ Apr 19 '14 at 08:22

4 Answers4

5

No. You don't even need variable names in the declaration of a function. Their purpose there is documentation.

Obviously this can be trivially checked:

void foo(int); // or void foo(int bar);

int main()
{
  foo(42);
}

#include <iostream>
void foo(int n)
{
  std::cout << n << std::endl;
}

In general, I can see no reason to use different names, and the cases where you can use no name in the declaration without loss of information are few and far between.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 1
    And the recommendation is?? – CouchDeveloper Apr 19 '14 at 08:34
  • @CouchDeveloper I wasn't aware OP was asking for recommendations. I would tend to use the same names, but sometimes a function with a clear name and a single parameter coud be declared without a parameter name without loss of information. For example, `double sqrt(double)`. – juanchopanza Apr 19 '14 at 08:37
  • Agreed. In particular, I would find using different names quite confusing. ;) – CouchDeveloper Apr 19 '14 at 08:42
1

Why give them different names?

If you are using a system such as Doxygen to document your code why confuse things.

Just be consistent and give them meaningful names.

In summary why make life hard for both you and the people using/maintaining your code

AN ASIDE Why do some programmers like to find the path of most resistance?!

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • The declaration might be in a header file over which you have no control, and your parameter name might be more descriptive, or conform to local coding conventions. – ClickRick Apr 19 '14 at 08:42
  • @ClickRick - Eh? Does the porogrammer write both ther header file and the definition (.cpp) file at the same time. I.e. adhere to one coding standards. Are you saying that some programmers have the luxury of just writing header files with no thought if the poor sod writing code to carry that contract out? – Ed Heal Apr 19 '14 at 08:46
1

No required. Even name of variable in function declaration is optional. But it helps user of function while passing parameter. For example,

void info(int,int);

and,

void info(int age,int salary);

Both are valid, but in first, you may not be sure about what value to pass, second gives clear indication.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
0

No, the prototype doesn't need to match the definition of a function (either it is a member function of a class or a simple function).

You can have it like this,

class myClass {
public:
    void myMethod (const int &name1);
}

And the definition.

void myClass::myMethod (const int &name2) {
    // use name2 here.
}

You can even skip the name of argument(s) (variable names) in the function prototype.

Like,

class myClass {
public:
    void myMethod (const int &);
}

Hope it helped you. If it doesn't. Read the related section in current C++ working draft.

Fahad Siddiqui
  • 1,829
  • 1
  • 19
  • 41