Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function declaration inside or outside the class

Tags:

c++

inline

I am a JAVA developer who is trying to learn C++, but I don't really know what the best practice is for standard function declarations.

In the class:

class Clazz {  public:     void Fun1()     {         //do something     } } 

Or outside:

class Clazz { public:     void Fun1(); }  Clazz::Fun1(){     // Do something } 

I have a feeling that the second one can be less readable...

like image 929
JohnJohnGa Avatar asked Jan 31 '12 07:01

JohnJohnGa


People also ask

Why do we declare function outside of class?

Defining a member function outside a class allows to separate the interface and its realization.

Can we define function inside the class?

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.

Can we declare function inside?

We can declare a function inside a function, but it's not a nested function. Because nested functions definitions can not access local variables of the surrounding blocks, they can access only global variables of the containing module.

How do we define function inside and outside of a class?

A member function can be defined inside the class body where it is declared. Function's entire body is defined inside class body. A member function can be defined outside the class. To define a function outside the class, scope resolution operator is used.


1 Answers

C++ is object oriented, in the sense that it supports the object oriented paradigm for software development.

However, differently from Java, C++ doesn't force you to group function definitions in classes: the standard C++ way for declaring a function is to just declare a function, without any class.

If instead you are talking about method declaration/definition then the standard way is to put just the declaration in an include file (normally named .h or .hpp) and the definition in a separate implementation file (normally named .cpp or .cxx). I agree this is indeed somewhat annoying and requires some duplication but it's how the language was designed (the main concept is that C++ compilation is done one unit at a time: you need the .cpp of the unit being compiled and just the .h of all the units being used by the compiled code; in other words the include file for a class must contain all the information needed to be able to generate code that uses the class). There are a LOT of details about this, with different implications about compile speed, execution speed, binary size and binary compatibility.

For quick experiments anything works... but for bigger projects the separation is something that is practically required (even if it may make sense to keep some implementation details in the public .h).

Note: Even if you know Java, C++ is a completely different language... and it's a language that cannot be learned by experimenting. The reason is that it's a rather complex language with a lot of asymmetries and apparently illogical choices, and most importantly, when you make a mistake there are no "runtime error angels" to save you like in Java... but there are instead "undefined behavior daemons".

The only reasonable way to learn C++ is by reading... no matter how smart you are there is no way you can guess what the committee decided (actually being smart is sometimes even a problem because the correct answer is illogical and a consequence of historical heritage.)

Just pick a good book or two and read them cover to cover.

like image 67
6502 Avatar answered Oct 22 '22 03:10

6502