Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple member function forward declarations

Tags:

c++

In C++ I can have multiple forward declaration of functions like:

void Func (int);
void Func (int);   // another forward declaration, compiles fine
void Func (int) {} // function definition, compiles fine

And yet VC++ 2010 complains when I do the same for member functions (whether or not I include a definition):

class Test {
   void Func (int);
   void Func (int);   // error C2535 here
   void Func (int) {} // error here too
   };

I couldn't find anything online about multiple member function forward declarations, whether its legal, illegal, VC++ specific, ect... Is there a way around this? Is it illegal?

Now why would I want to do that? No project in particular, was just playing around with different ways to register functions. In other projects I've had to register functions/classes and used less hack-ish but more tedious methods, and was just trying (for fun) different methods using macros/templates.

Any ideas or thoughts? Specifically on the above question, but also on registering functions/classes.

Thanks in advance for your time ;)

like image 616
Kaisha Avatar asked May 12 '11 15:05

Kaisha


People also ask

Can you forward-declare a function?

To write a forward declaration for a function, we use a function declaration statement (also called a function prototype). The function declaration consists of the function header (the function's return type, name, and parameter types), terminated with a semicolon. The function body is not included in the declaration.

When can I use forward declaration?

You will usually want to use forward declaration in a classes header file when you want to use the other type (class) as a member of the class. You can not use the forward-declared classes methods in the header file because C++ does not know the definition of that class at that point yet.

What are forward declarations in C?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

Why do we need forward declaration?

Forward declarations means the declaration of a method or variable prior to its implementation. Such declaration is necessary in C/C++ programming language in order to be able to use a variable or object before its implementation.


3 Answers

You can't have multiple declarations of a member function inside a class. Your code violates 9.3/2 of the C++ Standard which says

Except for member function definitions that appear outside of a class definition, and except for explicit specializations of member functions of class templates and member function templates (14.7) appearing outside of the class definition, a member function shall not be redeclared.

like image 186
Prasoon Saurav Avatar answered Nov 15 '22 19:11

Prasoon Saurav


There is no need to forward declare member functions. They are visible in the whole class anyway.

like image 27
Bo Persson Avatar answered Nov 15 '22 19:11

Bo Persson


As Mark B says, declaring free functions and declaring member functions is treated differently.

Free function declarations can be scattered all around the place, and it would be limiting to require that only one matching declaration be present in a program.

However, you can only define a class in one big chunk of class definition1, so all its member declarations are found in one place. They can't be scattered about your program; consequently, there's no reason for the standard to allow you to write multiple member declarations... so it doesn't:

Except for member function definitions that appear outside of a class definition, and except for explicit specializations of member functions of class templates and member function templates (14.7) appearing outside of the class definition, a member function shall not be redeclared. [9.3/2]


  1. Sure, you can include that class definition multiple times in a program, but it must match exactly so, for the purposes of this discussion, it might as well just be a single definition.
like image 32
Lightness Races in Orbit Avatar answered Nov 15 '22 19:11

Lightness Races in Orbit