Constructors...in the header or cpp file?

Started by
17 comments, last by Agape 20 years, 5 months ago
What''s the difference between defining the implementation of a constructor in a header or cpp file?
Advertisement
quote:Original post by Agape
What''s the difference between defining the implementation of a constructor in a header or cpp file?



Readability and consistancy.
quote:Original post by Agape
What''s the difference between defining the implementation of a constructor in a header or cpp file?



What''s the difference between defining the implementation of ANY function in a header or cpp file?
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
if defined inside class => unreadable
outside class but in .h => link errors (with more than one .cpp refering to the class which often occurs)
in .cpp => readable, no recompilation, no errors.. perfect
If the method is only a return statment, I define the method inline i.e. in the .h file:
    float getSpeed() const { return _fSpeed; }  

That's the only time it simpifies code reading, I think.

[edited by - Enselic on October 14, 2003 10:09:11 AM]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Any class methods declared and defined in the header file is implicitly made inline.
quote:Original post by deathtrap
Any class methods declared and defined in the header file is implicitly made inline.

Unless they''re not.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Putting the implementation in the header file creates unnecessary dependencies between classes. Keep the header file limited to interface, and put the implementation in the .cpp file. Do a Google search on "pimpl idiom" for a preferred way to do this.

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com
If you put it in the class itself, it tries to make it inline.
Zorx (a Puzzle Bobble clone)Discontinuity (an animation system for POV-Ray)
quote:Original post by dmikesell
Putting the implementation in the header file creates unnecessary dependencies between classes. Keep the header file limited to interface, and put the implementation in the .cpp file. Do a Google search on "pimpl idiom" for a preferred way to do this.

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com

Even in my case? In what way does it become unnucessary dependencies?
[s]--------------------------------------------------------[/s]chromecode.com - software with source code

This topic is closed to new replies.

Advertisement