-1

I have read that defining functions in headers is a bad practice, but how about classes? is defining them in the header files fine?

lombardo2
  • 131
  • 1
  • 2
  • 6
  • 1
    If you want users of your library to be able to make use of those classes, where else would you define them? – jogojapan Jan 09 '13 at 02:34
  • 3
    This is a duplicate of dozens of existing questions. – Oliver Charlesworth Jan 09 '13 at 02:34
  • @jogojapan, I guess you could argue that PIMPL sort of like defining the class elsewhere :p – chris Jan 09 '13 at 02:35
  • @chris PIMPL only affects public .h vs private .h typically yes? You would still define the class in a .h that is not exposed to client – Karthik T Jan 09 '13 at 02:37
  • @KarthikT, I was under the impression that the implementation went in the cpp. I've not used PIMPL myself, though. – chris Jan 09 '13 at 02:39
  • What does "defining a class" mean? I can *declare* a class (including its ctors, dtors, member functions, and member variables) and *define* a class' methods/functions by giving the executable code that runs when each function is called. Does the former or the later count as "defining the class"? – Code-Apprentice Jan 09 '13 at 02:39
  • @chris Hmmm.. I am not sure what it would mean to put an _entire_ class under the pimpl idiom. There would always have to be some public-facing class left that would need to go into the header, no? – jogojapan Jan 09 '13 at 02:41
  • 1
    @Code-Guru The _former_ is what I'd call a class definition. – jogojapan Jan 09 '13 at 02:41
  • @chris Ah, yes that would work too.. Even I have no first hand experience, so cant comment beyond that. – Karthik T Jan 09 '13 at 02:42
  • @Code-Guru you are confusing defining a class with defining a class's methods. – Karthik T Jan 09 '13 at 02:42
  • 1
    @jogojapan Then my answer is "yes". – Code-Apprentice Jan 09 '13 at 02:43
  • @jogojapan, Personally, I can't find an excuse to use it for compilation times, especially with the future modules that will become available. [This](http://stackoverflow.com/questions/60570/why-should-the-pimpl-idiom-be-used) seems like how one would typically implement the implementation to be private whilst defining the implementation completely in the cpp. – chris Jan 09 '13 at 02:46
  • @jogojapan As my initial comment hints, I dislike the word "define" (or any of its cognates) in this context. "implementation" and "interface" are much more precise, IMO. – Code-Apprentice Jan 09 '13 at 02:46
  • @chris Right.. the definition of `Cat` is still in the header, right? (Not the definition of all its functions, though.) – jogojapan Jan 09 '13 at 02:47
  • @Code-Guru I believe it's the C++ Standard that uses the word definition for this. – jogojapan Jan 09 '13 at 02:48
  • @jogojapan Sounds like I need to delve into the C++ standard when I have some spare time...and feel like getting back into C++ programming. – Code-Apprentice Jan 09 '13 at 02:50
  • @jogojapan, That's why I said sort of in my first comment. You're still defining a class in your header, but it's not the *real* class. – chris Jan 09 '13 at 02:51
  • @OP Perhaps you could clarify if pimpl as described by chris is what you are interested in. That would certainly be a reasonable question (although it may be a duplicate of the one chris linked). – jogojapan Jan 09 '13 at 02:54

1 Answers1

1

Unless this class is a pure template class or other class which is supposed to be inline, you'd better put class implementation into cpp files instead of header files. In a word, put interface in header files, while put implementations in cpp files.

P.S.

As @jogojapan said, I'm talking about the class implementation instead of class definition. That's because despite that OP is talking about class definition, I strongly doubt that he's actually meaning class implementation.

Hui Zheng
  • 9,306
  • 2
  • 29
  • 37