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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.