template functions in header

Pages: 12
closed account (zwA4jE8b)
So I understand that template functions need to be declared and defined in the same file.

My question is, in the following code, for example, do I need to bother with the function prototypes or should I just put the functions and definitions in the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Michael Ervin - Stack ADT

#ifndef H_STACK
#define H_STACK

template <class dType>
class stack
{
	int MAXSTACK;
	int _top;
	dType _init;
	dType* _stackarray;
public:
	stack(int _size, dType _initval);
	void initstack();
	void push(dType _topush);
	dType pop();
	bool isEmpty() const;
	bool isFull() const;
	int topstack() const;
	dType peek();
	~stack();
};

template <class dType>
stack<dType>::stack(int _size, dType _initval):_top(-1), _init(_initval), 	MAXSTACK(_size), stackarray(new dType[MAXSTACK])
{initstack();}

template <class dType>
void stack<dType>::initstack()
{
	for (int i = 0; i < MAXSTACK; i++) _stackarray[i] = _init;
}

template <class dType>
bool stack<dType>::isEmpty() const{	return (_top < 0);}

template <class dType>
bool stack<dType>::isFull() const{ return (_top >= MAXSTACK);}

template <class dType>
void stack<dType>::push(dType _topush)
{
	if (!isFull())
	{
		_top++;
		_stackarray[_top] = _topush;
	}
}

template <class dType>
dType stack<dType>::pop()
{
	dType _temp;
	if (!isEmpty())
	{
		_temp = _stackarray[_top];
		_stackarray[_top] = _init;
		_top--;
		return _temp;
	}
	else
		return NULL;
}

template <class dType>
int stack<dType>::topstack() const { return _top; }

template <class dType>
dType stack<dType>::peek() { return _stackarray[_top]; }

template <class dType>
stack<dType>::~stack(){	delete [] _stackarray;}
#endif 
My question is, in the following code, for example, do I need to bother with the function prototypes or should I just put the functions and definitions in the class.


It's your preference. Either one will work just fine.

The only difference is a style/code clarity issue. Some people would think keeping the functions outside the class is more organized because you can quickly see all members just by examining the class.
@Disch No, you're wrong about this one.
closed account (1vRz3TCk)
@Disch No, you're wrong about this one.
ceruleus, How so?
Last edited on
Someone should probably report me. In fact, I was thinking about it myself!
closed account (1vRz3TCk)
Someone should probably report me. In fact, I was thinking about it myself!
What would you like to be reported for?
closed account (zwA4jE8b)
Thank you disch.

I think hes being sarcastic, regarding all that drama with the other guys.
closed account (1vRz3TCk)
CreativeMFS,

You could also declare your templates in a header and put the implementation in another file, and include the implementation and the end of the header.
I like the style you chose to use! It's ok for very small classes to define their few methods inline, but then it's better to move them out of the body.

I do sometimes make an exception for the constructor and destructor. Unless the initializer list gets esp long.

Note that according to best practice, the order of member variables in the initializer list should be identical to order in which the variable are declared.

In the MFC / ATL world the inline functions are put in .inl files. But I wouldn't expect to see these in app development (cf. library development), as it's better to limit files to one class (expect for sets of tiny utility class, etc)

Andy

P.S. I have another couple of observations, but won't force them on you!
on a side note, you really shouldn't prefix variable names with an underscore.

Identifiers starting with an underscore is a convention reserved for compiler keywords, so you run the risk of colliding with a compiler keyword if you do this.
closed account (zwA4jE8b)
damn, I really liked that as a style thing to.
ew. why? =x

to each his own, I guess.
closed account (zwA4jE8b)
I don't really know why. It just appeals to me.
I always prefix member variables with m_, that's similar to your preference and also doesn't run the risk of clashing with compiler keywords.
CreativeMFS wrote:
that drama with the other guys

What drama are you talking about? It was a carefully planned
(from both sides) operation to teach other people two things:

(1) If you don't know the meaning of a word,
      google it instead of making assumptions.

(2) If you don't know whether someone is trolling or not, ask yourself
     "what's more probable based on his past behaviour?" before you act.

Even Grey Wolf's goodbye letter was a fake, as he's still here.
I really can't believe the fact that so many people fell for that
('that'  meaning  the  whole story, not just Grey Wolf's letter).
Last edited on
closed account (zwA4jE8b)
that is a pretty good idea quirky. maybe I will adopt something like that.
NOOOO GREY WOLF COME BACK **HOOOWWWLLLL**
closed account (D80DSL3A)
@m4ster r0shi. Goodbye letter is a fake? I wish he was still here but this post seems rather definite:

http://cplusplus.com/forum/lounge/47686/2/#msg260097

Is he here again under a new name?
Yes. And he lives in a self-balancing binary search tree.
closed account (1vRz3TCk)
Hmm, how very cryptic…

m4ster r0shi,

If I am him and he is me; what does it matter?

If I am not him and he not me; what does it matter?

If I say I not him, would you believe me? Probably not, so what does it matter?

So m4ster r0shi, what do you want?

Do you want me to say what a clever boy you are? Not going to happen.

Do you want me to leave to forum? I tell you what; I’ll put the power in your hands. Report this post and twicker can gladly close this account (as long as the report comes from you).

I have no interest in playing games with you, I have no interest in further interacting with you, and I have no interest in justifying myself to you. Let’s face it, I have no interest in you.

Last edited on
Pages: 12