C++ macro “if class is defined”

前端 未结 5 1932
孤独总比滥情好
孤独总比滥情好 2020-12-19 01:59

Is there such macro in C++ (cross-compiler or compiler-specific):

#if isclass(NameSpace::MyClass)

Would be useful.

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 02:10

    It sounds to me like it would be better to test if the header file with the class definition you're looking for has been included yet, instead of trying to see if the class exists. It's really easy to check this if you've been implementing the standard of defining a symbol for each header file, as shown:

    // myfile.h
    
    #ifndef _MYFILE_H_
    #define _MYFILE_H_
    
    // CODE
    
    #endif // _MYFILE_H_
    

    Your best bet though, is to just make sure your header files are being included in the right order in the first place. The easiest way to do this is to have an "overall" header file that in turn includes all the headers you will need in the correct order. Simply include that in each of the source files in your project, and you'll be good to go. This isn't necessarily the best solution, but it is the easiest.

提交回复
热议问题