Firstly, in my own defence I should point out that I'm something of a novice at C++.

Any help with the following would be gravelly appreciated.

I have a C++ class that holds an internal array of ints.
E.g:

class MyClass {
    public:
        .......  //lots of methods here
    private:
        #define STACKSIZE 100
        int stack[STACKSIZE];
};

I need many instances of this class, each with a different STACKSIZE. Rather than having multiple copies of this source, each with a different STACKSIZE define, I would like to somehow parametise its creation passing an argument to the compiler for STACKSIZE.

This seemed to me like a job for a C++ template. However, templates seem to be for 'types', so I can't see how to use one to solve this.

Thanks.
John Franklin

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Firstly, in my own defence I should point out that I'm something of a novice at C++.

Any help with the following would be gravelly appreciated.

I have a C++ class that holds an internal array of ints.
E.g:

class MyClass {
    public:
        .......  //lots of methods here
    private:
        #define STACKSIZE 100
        int stack[STACKSIZE];
};

I need many instances of this class, each with a different STACKSIZE. Rather than having multiple copies of this source, each with a different STACKSIZE define, I would like to somehow parametise its creation passing an argument to the compiler for STACKSIZE.

This seemed to me like a job for a C++ template. However, templates seem to be for 'types', so I can't see how to use one to solve this.

Thanks.
John Franklin

Hi john

templates are for types but tou can specify a default parameter eg

template <class T = int, int size = 100>
class MyClass {
    public:
        .......  //lots of methods here
    private:
        T stack[size];
};

you then declare it as

MyClass<int> m1 ;          gives a int stack with size 100
MyClass<int, 200> m2 ;          gives a int stack with size 200
MyClass<> m3 ;          gives a int stack with size 100

the bonus with this is that you could also declare a stack of double or even AnotherClass

Hope this helps

Stefan

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Hi john

templates are for types but tou can specify a default parameter eg

template <class T = int, int size = 100>
class MyClass {
    public:
        .......  //lots of methods here
    private:
        T stack[size];
};

you then declare it as

MyClass<int> m1 ;          gives a int stack with size 100
MyClass<int, 200> m2 ;          gives a int stack with size 200
MyClass<> m3 ;          gives a int stack with size 100

the bonus with this is that you could also declare a stack of double or even AnotherClass

Hope this helps

Stefan

Many thanks Stefan for going to the trouble of providing such a comprehensive and helpfull reply. This is axactly what I was after.

Also makes me realise how much I have yet to learn.

John

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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