简体   繁体   中英

Can I implemented all C++ member function in header file but not intended for inline?

For C programs, I know I need .h, and .cpp to allow multiple .h to be read and compiled together but linked later.

However, for C++, I don't want to break up the class member function declaration and definition to two parts. Although it is the typical C++ way, I am trying to merge member function declaration and definition in one header file. I saw some people doing that. I would like to know if there is any problem to do this.

  1. I know all members defined in .h class definition are then inline functions, but can they be non-inline in any way? Will compiler be controllable so that they don't have to be inline, for example, when the function is huge?
  2. The VS2015 intellisense shows error when the 2s is in a member function defined in header file, but doesn't show that in cpp file. But it compiles. Is it showing that I am doing it wrong, or it's a bug for VS2015? I deleted the sdf, restarted the project, as long as copy the function into the .h file, it shows a read line under std::this_thread::sleep_for(2s);

see

class testtest {
    void tryagain() {
        using namespace std::chrono_literals;
        auto twosec = 2s;
        std::this_thread::sleep_for(2s);// problem is here
    }
};
  1. Is compiling faster if we separate the .h and .cpp?
  2. My final compiler would be g++4.9.2, and I am using VS2015 to test build. The final code will run on an ARM with 1GB RAM on Linux.

I don't intend to argue about wich style is better, but hope to focus on whether it works, and what are the trade offs.

It is perfectly fine to place all implementation code in the .h file if you want, it usually depends on the situation. It's usually good practice to split up the interface and implementation even if all the code is in the .h file by doing something like this

class MyClass {
public:
    void doSomethingUseful();
};

....

void MyClass::doSomethingUseful() {
    // code goes here....
}

Not all functions will automatically be inlined, the compiler usually decides what to inline or not. Larger functions will likely not be inlined even if they're in the header. You can use the inline keyword to give a hint to the compiler that you'd like the function to be inlined but this is no guarantee (also as a small aside, functions marked as inline in a .cpp file will be inlined, but only when called from other functions within that same .cpp file)

Compile times will be slower with more code in the .h file so it's best to try and reduce the amount of code in them as much as possible. I guess in your case though it shouldn't be that noticeable.

I hope that is of some help! :)

You can put defines around functions to prevent them being implemented more than once header file:-

class MyClass {
public:
    void doSomethingUseful();
};
// inline functions

....
// non inline function
#ifdef MYCLASSCPP
void MyClass::doSomethingUseful() {
    // code goes here....
}
#endif

Then define MYCLASSCPP in just one CPP file. (or complier parameter).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM