Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

c++ - What are the advantages and disadvantages of implementing classes in header files?

I love the concept of DRY (don't repeat yourself [oops]), yet C++'s concept of header files goes against this rule of programming. Is there any drawback to defining a class member entirely in the header? If it's right to do for templates, why not for normal classes? I have some ideas for drawbacks and benefits, but what are yours?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Possible advantages of putting everything in header files:

  • Less redundancy (which leads to easier changes, easier refactoring, etc.)
  • May give compiler/linker better opportunities for optimization
  • Often easier to incorporate into an existing project

Possible disadvantages of putting everything in header files:

  • Longer compile/link cycles
  • Loss of separation of interface and implementation
  • Could lead to hard-to-resolve circular dependencies
  • Lots of inlining could increase executable size
  • Prevents binary compatibility of shared libraries/DLLs
  • Upsets co-workers who prefer the traditional ways of using C++

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...