Is this possible to declare a template struct similar to template class:

template<class type> typedef _stack
{
         type arr[MAX];
         int tos;
} stack;

Thanks.

Edited 6 Years Ago by Nick Evan: Added code-tags

Yes, but use classes. Prefer classes to structs.

template<typename Type>
class CStack
{
public: 
 const static int MAX = 100;
private:
Type _array[MAX];
size_t currentMaxIndex;
};

Edited 6 Years Ago by firstPerson: n/a

>Prefer classes to structs.
Do you have any particular reason for that guideline? I ask because I've met too many people that have some unreasonable idea of classes being more OO than structures. :icon_rolleyes: The only difference between structures and classes is default visibility (public for structures, private for classes). If you always label your visibility blocks, the only difference is the keyword ( class or struct ).

I personally prefer classes much of the time because I tend not to label my data member block:

class foo {
  // Data members here
  // Private by default
public:
  // Member functions here
  // Explicitly public
};

>> Do you have any particular reason for that guideline
I rather not have member variables public rather than private. But
occasionly I tend to use structs for private inner classes or as an helper. But judging what he is doing he should use classes.

>I rather not have member variables public rather than private.
Just because it's the default doesn't mean you have no choice. Using your example from earlier in the thread:

template<typename Type>
struct CStack
{
public: 
  const static int MAX = 100;
private:
  Type _array[MAX];
  size_t currentMaxIndex;
};

The only thing I changed was the keyword class to struct . The data members are still private because you used an explicit access section. Nothing else changed because the code doesn't use the default access rules.

>But judging what he is doing he should use classes.
He is using a class. It just happens to be defined with the struct keyword. You can put any philosophical spin you want on the differences, but to the compiler they're the same (the symbol table might even use the same symbol for both). You seem to be implying that there's a fundamental difference that makes classes more OO than structures, which is false. Might I suggest an argument that's far more convincing?

[Good Argument]
Using class is conventional in C++ for most user-defined types, so it should be preferred. struct is there for C compatibility and the class keyword was introduced to match Bjarne Stroustrup's concept of OOP. If you use struct instead of class , new programmers will be confused and experienced programmers will pause to wonder why you didn't follow convention.
[/Good Argument]

This is a practical argument that has a strong foundation in writing quality code. No idealism, no mumbo jumbo, just pragmatics that anyone should be able to understand.

Comments
Exactly!
Some clarity on the issue finally, i used to get all mixed up on that qs a lot of times

Ok, thats a better argument. At least I gained something out of this
conversation. I'm still learning as I go, because I just started "learning"
this C++ about a 1.5- 2.0 years ago. Thanks for clearing that up a bit more.

do any of you guys have any idea how to typedef this templated structure?

template<class type> struct _linked_list
{
	type value;
	struct _linked_list* next;
};

template<class type> typedef struct _linked_list<type> linked_list;
linked_list.c:7: error: template declaration of ‘typedef’

Thanks.

>do any of you guys have any idea how to typedef this templated structure?
You don't need to. That typedef strongly suggests that you're using a C-style trick to remove the need for the struct keyword. C++ automatically does it for you:

template<class type>
struct linked_list {
  type value;
  struct linked_list *next;
};

int main()
{
  linked_list<int> my_list; // Okay in C++
  struct linked_list<int> other_list; // Also okay
}

>linked_list.c:7: error: template declaration of ‘typedef’
Typedefs can't have template arguments. You need to specify an explicit type for the instantiation.

>> typedef this templated structure

Why? Just name your class appropriately. And you will be fine. Also check this out for more info.

Edited 6 Years Ago by firstPerson: n/a

This article has been dead for over six months. Start a new discussion instead.