Forward declares C ++ static function

I want to forward the declaration of a static member function of a class in another file. What I want to do looks like this:

BigMassiveHeader.h:

class foo { static void init_foos(); } 

main.cpp:

 class foo; void foo::init_foos(); int main(char** argv, int argc) { foo::init_foos() } 

This failed with error C2027: using undefined type 'foo' "

Is there a way to accomplish what I want to do to make init_foos a free function or to include BigMassiveHeader.h? (BigMassiveHeader.h noticeably affects compilation time and is included everywhere.)

+7
c ++
source share
8 answers

You cannot redirect declared members of a class, whether they are static or not.

+10
source share

You cannot redirect declarations of members of your class, but you can create a namespace and function inside that namespace and forward the declaration.

 namespace nsfoo { void init_foos(); } 

If necessary, your class can be friends with this function.

+4
source share

If you have a BigMassiveHeader , you should consider splitting it into several SmallCompactHeaders . If you want to express that many classes and functions belong semantically, you can put them in the same namespace. You can always provide a convenience header that includes all of your small headings.

+2
source share

No, you need to include a title for this. Unfortunately.

Use a free function if you must or split the class.

0
source share

You cannot partially declare classes in C ++, so you have to put the class declaration in your own, smaller header, or ...

Include BigMassiveHeader.h in your file and use precompiled headers. Visual C ++ Method: http://msdn.microsoft.com/en-us/library/2yzw0wyd%28v=VS.71%29.aspx or GCC Method: http://gcc.gnu.org/onlinedocs/gcc /Precompiled-Headers.html .

0
source share

I know this is not a question, but if BigMassiveHeader.h is unlikely to change much over time, you should take a look at the precompiled headers

0
source share

As the first refactoring, I would use a free function that calls a static function. This is not like your main method has been called many times, so you will not notice an extra call, and this makes a minimal change to the existing code.

Of course, you are not really saying what you are trying to do, only what you want to do. If what you are trying to do is get init_foos once when you start the application, use the static initialization of the object for this, rather than calling it in main. If what you are trying to do is get init_foos after all the static objects are created, then it will be harder.

By static initialization of an object, I mean something like this in a .cpp file that has access to the init_foos definition. Make it friend and init_foos private to prevent multiple calls:

 struct call_init_foos { call_init_foos () { foo::init_foos(); } } call_init_foos_on_startup; 
0
source share

in order to forward the declaration of one method of a class, you must declare this method as part of the class (as it really is).

for example, in your case, add to main.cpp:

 class foo { public: static void init_foos(); } 

its not the most beautiful, but it will save you from including the whole title.

0
source share

All Articles