Where should I implement my class method?

Nick

I aware of three different kind of implementation "locations" for my class methods:

1) Define the method inside my class (.h file) and implement it in my .cpp file

//.h
class Foo
{
  int getVal() const;
};


//.cpp
int Foo::getVal() const
{ return 0; }

2) Define and implement the method inside my class (.h file).

//.h
class Foo
{
  int getVal() const
  { return 0; }
};

3) Define the method inside my class and implement it outside the class but inside my header file.

//.h
class Foo
{
  int getVal() const;
};

int Foo::getVal() const
{ return 0; }

What are the main differences between these three approaches?

IdeaHat

There are three elements to this question: readability (how good the code looks), compilation (how much the compiler can optimize it) and implementation hiding (If you use the code as a library, you may not want to explicitly share your special sauce with the world).

Method one exposes only the interface for the function in the header file. This means you show a nice clean interface, and your implementation is not exposed in plaintext. However, the code cannot be inlined across compilation units, so it has the potential to be a little slower in runtime (in practice, this only matters for a very, very very small percentage of the code). THIS SHOULD BE YOUR DEFAULT WAY TO GO.

Method 2 is implicit inlining. Long functions will clutter up your class which (imho) is bad. Also exposes your implementation to the world. However, the function can be inlined and is less verbose than defining it in another place. I reserve this for very small functions.

Method 3 is actually illegal, as you will break the one-definition rule, but the following is fine:

//Foo.h
class Foo {
  int getVal() const;
};

inline int Foo::getVal() const { 
  return 0; 
}

I use this when I want to keep the class definition clean but want the function definition in the header file (for inlinable or template functions).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In the Repository Pattern, where should I implement a Distinct method?

From Dev

Where should I implement onClick method in a NavigationDrawer Activity?

From Dev

Where should I put a custom method for a existing class in rails?

From Dev

How should I declare that my method require any class?

From Dev

Should I compare all fields in my class's "equals" method?

From Dev

Should I use the class or instance method to increment my object?

From Dev

Should I be concerned if my rails class is overridden by a reserved public method?

From Dev

where should I implement SeekBar.onSeekBarChangeListener?

From Dev

where should I implement SeekBar.onSeekBarChangeListener?

From Dev

Should I implement equals and hashCode in a domain class?

From Dev

Where should implement the methods of AsyncTask class if have multiple classes in Android?

From Dev

Where should I use release() method in android?

From Dev

Where should I manage the coherency of my database?

From Dev

Where should I store my data models?

From Dev

Where should I place my lib tests

From Dev

Where should I place my global macros?

From Dev

Where should I keep my shader code?

From Dev

Where should I keep my test assets?

From Dev

Where should I mount my external harddrive?

From Dev

Where should I put my bash scripts

From Dev

Where should I put my defaults?

From Dev

Where should I install my app on Linux?

From Dev

Where should i access my Database

From Dev

Where should I put my source code?

From Dev

Where should I place my bootloader?

From Dev

where I should print my results

From Dev

Where should I put my lang attribute?

From Dev

Should I always implement the dispose pattern if my class defines a field in which the field’s type implements the dispose pattern? c#

From Dev

where should I implement security/Authorization in n-tier architecture?

Related Related

  1. 1

    In the Repository Pattern, where should I implement a Distinct method?

  2. 2

    Where should I implement onClick method in a NavigationDrawer Activity?

  3. 3

    Where should I put a custom method for a existing class in rails?

  4. 4

    How should I declare that my method require any class?

  5. 5

    Should I compare all fields in my class's "equals" method?

  6. 6

    Should I use the class or instance method to increment my object?

  7. 7

    Should I be concerned if my rails class is overridden by a reserved public method?

  8. 8

    where should I implement SeekBar.onSeekBarChangeListener?

  9. 9

    where should I implement SeekBar.onSeekBarChangeListener?

  10. 10

    Should I implement equals and hashCode in a domain class?

  11. 11

    Where should implement the methods of AsyncTask class if have multiple classes in Android?

  12. 12

    Where should I use release() method in android?

  13. 13

    Where should I manage the coherency of my database?

  14. 14

    Where should I store my data models?

  15. 15

    Where should I place my lib tests

  16. 16

    Where should I place my global macros?

  17. 17

    Where should I keep my shader code?

  18. 18

    Where should I keep my test assets?

  19. 19

    Where should I mount my external harddrive?

  20. 20

    Where should I put my bash scripts

  21. 21

    Where should I put my defaults?

  22. 22

    Where should I install my app on Linux?

  23. 23

    Where should i access my Database

  24. 24

    Where should I put my source code?

  25. 25

    Where should I place my bootloader?

  26. 26

    where I should print my results

  27. 27

    Where should I put my lang attribute?

  28. 28

    Should I always implement the dispose pattern if my class defines a field in which the field’s type implements the dispose pattern? c#

  29. 29

    where should I implement security/Authorization in n-tier architecture?

HotTag

Archive