Alternatives to template classes in C ++?

Is it possible to define common classes without using templates in C ++. I wrote a compiler for a language that generates C ++ code, but I did not include a template construct in my language.

What I want to know is the opportunity for general classes in my language and with my language.

Let's try to do it as follows:

To implement a new data structure (for example, stack), I declare its type as "Data", an empty class, and imposed a restriction on the user to first define a new class (for example, myData) that extends the data and uses only myData objects with a data structure.

Stack.h definition:

#ifndef STACK_H
#define STACK_H

#include <stack>

class Data{};

class Stack {
private:
    std::stack<Data*> st;
public:
    bool empty()    {return st.empty();}
    int size()  {return st.size();}
    void pop()  {st.pop();}
    Data* top() {return st.top();}
    void push(Data &e) {st.push(&e); }
};

#endif

The expected definition of "myData" from the user:

class myData: public Data{
public:
    int a;
    myData (int A): a(A){}
    myData (){}
    myData (const Data& x) {
    }

    myData& operator= (const Data& x) {
    }
    int getData(){return a;}
};

Tried to run the following main ():

int main(){
    stack<Data*> st;
    myData A(10);
    st.push(&A);
    printf("%u\n", &A);
    printf("Size: %d\n", (int)st.size());

    myData B;
    Data *C = (st.top());
    B = *((myData*)C);
    printf("%u\n", C);
    printf("%d\n", B.getData());

    return 0;
}

, , , . (++ - {template classes}).

+4
1

, ++ , .

-1

All Articles