learnvst
learnvst

Reputation: 16193

Why are function argument names unimportant in c++ declarations?

Function argument names in declarations (that most likely reside in the header file) are seemingly completely ignored by the compiler. What are the reasons for allowing the following to compile using either declaration version 1 or 2?


implementation

void A::doStuff(int numElements, float* data)
{
    //stuff
}

declaration - Version 1

class A
{
public:
    void doStuff(int numElements, float* data);
}

declaration - Version 2

class A
{
public:
    void doStuff(int, float*);
}

Upvotes: 6

Views: 859

Answers (5)

Martin James
Martin James

Reputation: 24907

..because when the headers are included in other modules it only needs the types to generate the correct code. The names ae often useful and convenient, but nopt absolutely necessary.

Upvotes: 1

Benjamin Schulte
Benjamin Schulte

Reputation: 873

The compiler only needs to know what kind of arguments the method requires. It's unimportant for the compiler how you call them.

The compiler needs to know the argument types for several reasons:

  • Decide which method to use if there are several methods with the same method name
  • Decide whether the input parameters are valid
  • Decide whether the parameters need to be casted
  • Decide how to generate the CODE to call the method and handle the response

However, I suggest to use the first header version. It helps other developers (and yourself) to use the functions and know what parameters have which meaning.

Upvotes: 11

Luchian Grigore
Luchian Grigore

Reputation: 258678

Parameter names aren't part of the function signature. Unless you use them, you don't need to have names even in the function implementation.

Upvotes: 6

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272802

Because the names don't affect anything the compiler does outside the function.

Upvotes: 5

MByD
MByD

Reputation: 137442

The only reason I can think about that version 1 is better is readability. They are ignored as they don't matter for the compiler.

Upvotes: 1

Related Questions