Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding declaring private functions in class header files (C++)

Tags:

(In C++) I have a class whose structure is declared in a header file. That header file is included in lots of source files, such that when I edit it I need to recompile lots of files.

The class has a set of private functions which are only called in one source file. Currently they are declared in the class structure in the header file. When I add a new function of this type, or edit the arguments, it therefore causes recompilation of lots of files. I would like to declare the functions somewhere else, such that only the file that defines and calls them is recompiled (to save time). They still need to be able to access the internal class variables, though.

How can I achieve this?

like image 941
user664303 Avatar asked Apr 14 '11 15:04

user664303


People also ask

Why do we declare a class private member variables in a header file?

The primary reason this is needed is that any code that uses a class needs to know about private class members in order to generate code that can handle it.

Can class functions be private?

Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.

Do class definitions go in header file?

The header file has the extension of . h and contains class definitions and functions.

Should each class have its own header file?

Each class shall have it's own header and implementation file. So if we wrote a class called MyString we would need an associated MyStringh.


2 Answers

Use the pImpl idiom - Your visible class keeps a pointer to the real class and forwards calls to public member functions.

EDIT: In response to comments

// Foo.h:  class FooImpl; // Do *not* include FooImpl.h class Foo { public:   Foo();   ~Foo();   //.. also need copy ctor and op=   int bar(); private:   FooImpl * Impl; };  // FooImpl.h:  class FooImpl { public:   int bar() { return Bar; } private:   int Bar; };  // Foo.cpp:  #include "FooImpl.h"  Foo::Foo() { Impl = new FooImpl(); } Foo::~Foo() { delete Impl; } int Foo::bar() { return Impl->bar(); } 

Keep the actual implementation of your class in FooImpl - Foo should have copies of the public members of FooImpl and simply forward calls to these. All users will include only "Foo.h" - you can change all the private details of FooImpl without the users of Foo seeing any changes.

like image 72
Erik Avatar answered Oct 11 '22 09:10

Erik


There is no way to declare member functions of a class outside the main class declaration. So, if you want to declare, outside of the class in question, functions that can access member variables of a particular instance of the class, then I see no alternative but to pass that instance to the function. Furthermore, if you want the functions to be able to access the private and protected variables you will need to put them in a new class and make the original class a friend of that. E.g.

header.h:

class FooImpl;  class Foo { public:    int bar();    friend class FooImpl; private:    int var; } 

impl.cpp:

#include "header.h"  class FooImpl { public:    int bar(Foo &); }  int FooImpl::bar(Foo &foo) { return foo.var; }  int Foo::bar() { return FooImpl::bar(*this); } 
like image 31
user664303 Avatar answered Oct 11 '22 11:10

user664303