How can I avoid including class implementation files?

前端 未结 7 707
轮回少年
轮回少年 2020-11-29 11:45

Instead of doing

#include \"MyClass.cpp\"

I would like to do

#include \"MyClass.h\"

I\'ve read online tha

相关标签:
7条回答
  • 2020-11-29 12:24

    In addition to hiding implementation details in cpp files (check other replies), you can additionally hide structure details by class forward declaration.

    class FooPrivate;  
    
    class Foo  
    {  
      public:  
      // public stuff goes here  
      private:  
      FooPrivate *foo_private;  
    };
    

    The expression class FooPrivate says that FooPrivate is completely defined somewhere else (preferably in the same file where Foo's implementation resides, before Foo's stuff comes. This way you make sure that implementation details of Foo(Private) aren't exposed via the header file.

    0 讨论(0)
  • 2020-11-29 12:24

    You needn't include .c or .cpp files - the compiler will compile them regardless whether they're #included in other files or not. However, the code in the .c/.cpp files is useless if the other files are unaware of the classes/methods/functions/global vars/whatever that's contained in them. And that's where headers come into play. In the headers, you only put declarations, such as this one:

    //myfile.hpp
    class MyClass {
        public:
            MyClass (void);
            void myMethod (void);
            static int myStaticVar;
        private:
            int myPrivateVar;
    };
    

    Now, all .c/.cpp files that will #include "myfile.hpp" will be able to create instances of MyClass, operate on myStaticVar and call MyClass::myMethod(), even though there's no actual implementation here! See?

    The implementation (the actual code) goes into myfile.cpp, where you tell the compiler what all your stuff does:

    //myfile.cpp
    int MyClass::myStaticVar = 0;
    
    MyClass::MyClass (void) {
        myPrivateVar = 0;
    }
    
    void MyClass::myMethod (void) {
        myPrivateVar++;
    }
    

    You never include this file anywhere, it's absolutely not necessary.

    A tip: create a main.hpp (or main.h, if you prefer - makes no difference) file and put all the #includes there. Each .c/.cpp file will then only need to have have this line: #include "main.hpp". This is enough to have access to all classes, methods etc. you declared in your entire project :).

    0 讨论(0)
  • 2020-11-29 12:24

    One thing you will want to watch out for when including you class declarations from a .h/.hpp is make sure it only ever gets included once. If you don't do this you will get some possibly cryptic compiler errors that will drive you up the wall.

    To do this you need to tell the compiler, using a #define, to include the file only if the #define does not already exist.

    For example (MyClass.h):

    #ifndef MYCLASS_H
    #define MYCLASS_H
    class MyClass 
    {
    // Memebers and methods
    }
    #endif
    // End of file
    

    This will guarantee your class declaration only gets included once even if you have it included in many different .cpp files.

    0 讨论(0)
  • 2020-11-29 12:27

    You should not include a source file (.c or .cpp). Instead you should include the corresponding header file(.h) containing the declarations. The source files needs to be compiled separately and linked together to get the final executable.

    0 讨论(0)
  • 2020-11-29 12:31

    This is called separate compilation model. You include class declarations into each module where they are needed, but define them only once.

    0 讨论(0)
  • 2020-11-29 12:31

    Cpp files should be defined in your compiler script to be compiled as object files.

    What ide are you using? I am going to assume you are compiling with gcc, so here is the command to compile two .cpp files into one executable

    gcc -o myclasses.out myclass.cpp myotherclass.cpp
    

    You should only use #include to include class definitions, not the implentation

    0 讨论(0)
提交回复
热议问题