0

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.

  • 1
    Look at how your implementation has provided `std::vector` in the header file. – Richard Critten Jan 24 '22 at 23:42
  • 1
    Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Richard Critten Jan 24 '22 at 23:43
  • 1
    Personal choice, but for a template I leave the implementations inside the class because the extra complications from separating them usually outweigh the advantages of separating them. Exceptions: I'm doing some really groovy explicit specialization or the function is particularly long. – user4581301 Jan 24 '22 at 23:44
  • @user4581301 You mean right inside the class definition or inside the header file but outside the class? – DirichletIsaPartyPooper Jan 24 '22 at 23:45
  • 3
    Avoid `#include "DynamicArray.cpp"` with a heavy trend toward never do it. Rational: Some build tools and many people will see the .cpp extension, compile the file and link it, usually resulting in linker errors when the linker finds the same definitions over and over again in many object files. – user4581301 Jan 24 '22 at 23:46
  • 1
    I put the functions right in the class unless I'm given a good reason not to. If you separate them and have to change a prototype, you have to make two changes. Forget one and you get compiler or linker error messages depending on which one you messed up. – user4581301 Jan 24 '22 at 23:47
  • @user4581301 Do you implement any function of the template class inside the class, or just those that uses the template type? – DirichletIsaPartyPooper Jan 25 '22 at 01:32
  • I'm not sure what you mean by that. – user4581301 Jan 25 '22 at 02:12
  • @user4581301 I mean, in the template class there are functions that use the template type T (for example as a returned value), which makes them template functions. So my question is what do you do with the regular functions that do not use the template type (such as seters or geters of fields of types other than T) – DirichletIsaPartyPooper Jan 25 '22 at 02:17
  • 1
    All of the class members are class members. If the class is templated, the member functions are templated. Example: https://godbolt.org/z/MWe5Tnr4Y – user4581301 Jan 25 '22 at 04:17

1 Answers1

3

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.

Martin York
  • 257,169
  • 86
  • 333
  • 562