Re: Which is the better way to define methods?

From: E. Robert Tisdale (E.Robert.Tisdale_at_jpl.nasa.gov)
Date: 07/08/04


Date: Wed, 07 Jul 2004 19:37:52 -0700

Blue Ocean wrote:

> E. Robert Tisdale wrote:
>
[snip]
>>
>>You need to *unlearn* your Java habits.
>>If you define constructors, functions or operators
>>within the class definition, they are inline functions.
>>You can move the function definition out of the class definition
>>but you must qualify it with the 'inline' keyword
>>or move the definition into a separate *implementation* source file
>>where it will be compiled exactly *once*!
>
> So . . . I wasn't thinking at all about inline functions.
> If a method is defined inside the class definition, that's what happens?
> So, tell me if this is right (for normal, non-inlined methods:
>
>
> ========================================
           #ifndef guard_file_h
           #define guard_file_h
> // file.h
>
> class Stack {
> private:
> int somethingoranother;
>
> public:
> Stack(void);
> ~Stack(void);
> int foo(double bar);
> };
>
           #endif// guard_file_h

> // file.cpp
           #include "file.h"
>
> int Stack::foo(double bar) {
> // Behavior
> }
>
> Stack::Stack(void) {
> // Behavior
> }
> ========================================
>
> If that is right, let me extend the question a little.
> Should I declare private methods in the .h file?
> Or should I declare them in the .c file?

You must *declare* them in the class definition.
You may *define* them in the header file as *inline* functions.



Relevant Pages