Poll

Inline or outside?

Inline
5 (71.4%)
Outside class
2 (28.6%)
Other
0 (0%)

Total Members Voted: 7

Author Topic: Class Member Functions inline or outside of class (Programming/ maybe C++)  (Read 913 times)

Inline:

Code: [Select]
class Box {
   public:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
   
      double getVolume(void) {
         return length * breadth * height;
      }
};

Outside of the class:

Code: [Select]
class Box {
   public:
      double length;         // Length of a box
      double breadth;        // Breadth of a box
      double height;         // Height of a box
      double getVolume(void);// Returns box volume
};

Code: [Select]
double Box::getVolume(void) {
   return length * breadth * height;
}

I feel like using it outside of the class just makes everything way more messy, as you have to have two lines (one for the declaration and one for the actual function). What do you think? If you don't know what I'm talking about, just answer Inline because that one is correct.

If you have to declare it inside the class before you can define it outside then there's no point in doing it outside.

But for any language that lets you declare it outside then that's pretty cool. Allows for some more dynamic approaches to things.

Declaring and implementing it inside the class is bad for implementation hiding and encapsulation and is against OOP design principles.

(For C++ and similar languages) The class and all it's member functions should be declared in the header file, whereas the implementation should always be defined in a seperate .cc or .cpp file.

There are exceptions to this rule in C++ though, because there are some member functions that NEED to be implemented in the class definition (some overloads).

For more reading on the subject I suggest you read a website on OOP/design patterns and this will make more sense.

ALSO, in C++ specifically, if you declare and implement a member function in the class definition, it counts as an inline function to the compiler.

An inline function is semantically different from a normal function so it can affect your runtime memory/compilation time.

I choose outside, why?

Because that makes the code more readable and structured at the same time.

How?

You know how a C code is structured right? Headers(foo.h) and the C code itself (foo.c).

The main purpose of the Header(foo.h) is to give a synopsis of the defined symbols(variables and functions) in foo.c, while the foo.c  will be used to "describe" such symbols(variables and functions).

A class is composed of variables and functions(symbols), the c code contains the description of the symbols while the Header contains the synopsis of them.

I don't think inline or outline break any encapsulation guidelines. A public routine should be a hidden black box from other classes and should do their job when interfacing but not get all up in their business. Putting the routine inside or outside of the class declaration has no effect on the way the code is refactored in the future or implemented by other classes. If anything putting the routine inside the declaration probably reduces member lifetime because there are less lines between the declaration of a member and it's use. Member lifetime is directly proportional with errors according to several studies

I only used Java and c# mostly and in those you're required to have your class routines inside the class block. It makes more sense anyways because both data and behavior is the building block of a class so naturally they should be together. Both C and C++ are incredibly ass backwards in syntax and coding practices and all the low level implementation. If the common practice is to use header and source files then I guess you should design your code around that
« Last Edit: July 17, 2018, 06:02:55 PM by thegoodperry »

I don't think inline or outline break any encapsulation guidelines. A public routine should be a hidden black box from other classes and should do their job when interfacing but not get all up in their business. Putting the routine inside or outside of the class declaration has no effect on the way the code is refactored in the future or implemented by other classes. If anything putting the routine inside the declaration probably reduces member lifetime because there are less lines between the declaration of a member and it's use. Member lifetime is directly proportional with errors according to several studies

I only used Java and c# mostly and in those you're required to have your class routines inside the class block. It makes more sense anyways because both data and behavior is the building block of a class so naturally they should be together. Both C and C++ are incredibly ass backwards in syntax and coding practices and all the low level implementation. If the common practice is to use header and source files then I guess you should design your code around that
Interesting points, but most of your first paragraph is untrue for C++.

tl;dr this entire thread:
It depends on which language you are using frankly, as C++ was designed differently to Java, C#, etc. so the recommended way of implementing things are different.
« Last Edit: July 17, 2018, 06:32:16 PM by Aide33 »

Brainf*ck doesn't have classes, but I program in C++, and I use inline for that.

lol brainforget is the worst turing tar pit Ive ever seen in my life. absolutely disgusting
« Last Edit: July 19, 2018, 02:02:33 PM by thegoodperry »

lol brainforget is the worst turing tar pit Ive ever seen in my life. absolutely disgusting

Well so are you.