Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Member function definition

What is the right approach to take:

Define the member (class) function inside the class?

Define the member (class) function outside the class?

Thanks.

like image 266
Simplicity Avatar asked Jan 29 '11 14:01

Simplicity


People also ask

How do you define member function?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.

What is member function and data member?

Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class.

What is member function class 12?

Answer: The member functions are behaviour of a class and can access or manipulate data members without passing them as parameters.

What is object and member function?

Object -- an encapsulation of data along with functions that act upon that data. An object consists of: Name -- the variable name we give it. Member data -- the data that describes the object. Member functions -- behavior aspects of the object (functions related to the object itself)


1 Answers

Assuming you're talking about these three possibilities:

  1. Method defined in class definition in header file.
  2. Method define outside class definition in header file.
  3. Method define outside class definition in implementation file.

Then project and company guidelines may force you to use (1) or (3) always.

When you have a choice, it's IMHO best to adapt to circumstances at hand, considering things such as

  • Do you want a header-only module? Then (1) as default, (2) possible.
  • Is the method a large beast? Then (2) or (3).
  • Template method specialization? Then (2) or (3).
  • There is a build-time problem (slow builds)? Indicates (3).
  • Template class? (1) or possibly (2)

But except where the choice is effectively forced on you, above all consider the clarity of your code.

Cheers & hth.,

like image 74
Cheers and hth. - Alf Avatar answered Oct 03 '22 08:10

Cheers and hth. - Alf