Multistage feature for forward announcements

In C ++, I can have multiple declarations of the following functions:

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

And still, VC ++ 2010 complains when I do the same for member functions (did I include the definition):

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

I could not find anything on the Internet about announcements with several member functions, be it their legal, illegal, specific VC ++, ect ... Is there any way around this? It is illegal?

Now, why do I want to do this? Not a single project, in particular, just played with different ways of registering functions. In other projects, I had to register functions / classes and use less hack-ish, but more tedious methods and just tried (for fun) to use different methods using macros / templates.

Any ideas or thoughts? In particular, on the above question, as well as on the registration of functions / classes.

Thanks in advance for your time;)

+8
c ++
source share
3 answers

You cannot have multiple member function declarations within a class. Your code violates 9.3/2 the C ++ standard, which says

With the exception of the definitions of member functions that appear outside the definition of a class and with the exception of explicit specializations of member functions of the class, templates and templates of member functions (14.7) that go beyond the definition of a class, the member function should not be redesigned.

+11
source share

There is no need to forward declared member functions. They are still visible throughout the class.

+5
source share

As Mark B notes, declaring free functions and declaring member functions is handled differently.

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

However, you can only define a class in one large class of a class 1 definition, so all of its member declarations are in one place. They cannot be scattered about your program; therefore, there is no reason for the standard to allow you to write multiple member declarations ... so this is not so:

With the exception of the definitions of member functions that appear outside the class definition, with the exception of the explicit specialization of member functions, class templates and member function templates (14.7) appearing outside the class definition, the member function should not be redefined. [9.3 / 2]


  • Of course, you can include this class definition several times in the program, but it should correspond exactly so, for the purposes of this discussion, it may just be the only definition.
+1
source share

All Articles