P
P
Prizm2020-12-16 22:01:54
OOP
Prizm, 2020-12-16 22:01:54

Specifying the type of a C++ child class?

I have a partially abstract class and several of its children (let's call them A, A1, A2, .. respectively).
In one method A, I need to define the "id" of the class from which it is called. If called from an instance of A1, then this value is 1, for A2, and so on. for A, this value is not required to be defined (well, or it is simply 0).
To do this, I found two solutions:
1:

//A:
friend virtual int getType() = 0; 
//A1:
int getType() {
    return 1;
}

But it doesn't look very good.

2:
template<int D>
class A {
...
};
class A1 : public A<1>{
...
};

but because of temlate, the method is not credible (because I learned about them just a couple of days ago).
Which of these methods is better to use (and why it)? Or is it better to come up with something else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuriy Vorobyov, 2020-12-16
@PrizmMARgh

Templates are a completely applicable thing that is needed in the language and helps to solve a number of tasks, no problems in use - no

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question