Home > database >  What is the best way to implement mehtods of generic class (c )
What is the best way to implement mehtods of generic class (c )

Time:01-25

I want to implement a class of dynamic array in c , and I want this implementation to be generic.

Consider the following definition:

ef DYNAMICARRAY_H
#define DYNAMICARRAY_H

template<class T> 
class DynamicArray
{
    public:
        DynamicArray();
        virtual ~DynamicArray();

    protected:

    private:
};

#endif // DYNAMICARRAY_H

(I did not wrote any methods yet).

Usually, we implement the methods in another cpp file. But since it is a generic class, we'll have problem with the linker after compilation with methods that use the generic types.

On the other hand, as I understand, implementing such a function in the header file might cause the compiler to make the function an inline function.

So what would be the best way to implement such functions? should it be inside the class definition? (case1) should it be in outside the class definition but inside the header file? (case2)

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

template<class T> 
class DynamicArray
{
    public:
        DynamicArray();
        virtual ~DynamicArray();

    protected:

    private:
        
        //Should I implement here right after decleration? (case 1)
};

    //Or should I implement here outside of the class definition? (case2)

#endif // DYNAMICARRAY_H

Or maybe in the cpp file and to include the line:

#include "DynamicArray.cpp"

above the main function?

Thanks in advance.

CodePudding user response:

On the other hand, as I understand, implementing such a function in the header file might cause the compiler to make the function an inline function.

Note 1: All functions defined in the class are "inline".
Note 2: All functions in the header files (not in the class) should have the inline keyword added by the engineer manually to prevent linker errors.
Note 3: An "inline" function/method has very little to do with "inlineing" the code in modern compilers. This is a holdover from ancient times when engineers were expected to be more knowledgeable than the compiler about the underlying hardware. Nowadays, this does not hold (usually).

So what would be the best way to implement such functions? should it be inside the class definition? (case1) should it be in outside the class definition but inside the header file? (case2)

Case 1: Yes that is valid.
Case 2: Yes that is also valid.

Which you should do is personal preference (depends on your coding standard). What is best is situational and dependent on circumstance.

Or maybe in the cpp file and to include the line:

Case 3: No. That is a bad idea.

Though I have seen people put the code in a "*.tpp" file that is included at the bottom of the "*.h" file. Probably best avoided.

above the main function?

Case 4: If your project is self-contained only has one source file, and you don't plan using the header file anywhere else, then sure. Otherwise, no.

  •  Tags:  
  • Related