• Advertisement

Archived

This topic is now archived and is closed to further replies.

Constructors...in the header or cpp file?

This topic is 5283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What''s the difference between defining the implementation of a constructor in a header or cpp file?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
quote:
Original post by deathtrap
Any class methods declared and defined in the header file is implicitly made inline.

Unless they''re not.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
If you put it in the class itself, it tries to make it inline.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

  • Advertisement