G
Gonçalo Rodrigues
Hi all,
I'm pretty new to C++ although I have a moderate experience with
programming, especially with Python and, to a lesser extent, with Java
and C.
By necessity I needed to abandon my lovely and flexible Python for...
uh, let's just say the more difficult C++. I've been trying to
understand templates and while I understand the concept I'm not being
able to do anything useful with them.
My problem(s) can be summarized in the folowing simple example:
Suppose you are writing a bunch classes (not necessarily related by
any inheritance relationships) that provide operator== and operator!=
but the latter is always like this
bool <type>:perator!=(const <type>& other) const {
return !(*this == other);
}
It would be nice to have a template that acted like a sort of mixin
and automatically introduced the above source code in the class
definition (and that's what templates are for, source code reuse).
The template is
template<typename T>
class Comparable {
public:
//Assumes operator==.
bool operator!=(const T& other) const {
return !(*this == other);
}
}
And now the class is
class Object {
public:
bool operator==(const Object& other) const {
//Compare by identity.
return this == &other;
}
The leap I'm not being able to make is how to write the Object class
so that the template Comparable gets "mixed in".
A related question: suppose now you are writing a bunch of classes
that also provide the order operators but where all of them are
defined in terms of operator< and operator==, e.g.
bool <type>:perator<=(const <type>& other) const {
return (*this == other) || (*this < other);
};
and similarly for operator> and operator>=. How can I take advantage
of the Comparable template and somehow write a template that inherits
from it? Is it
template<typename T>
class Orderable : public Comparable<T> {
//etc.
}
And assuming the above is correct (that is, it does what I tried to
explain above) does it imply any inheritance relationships for its
intantiations? e.g Orderable<int> is a subclass of Comparable<int>? If
this is the case, is there any possibility of writing the template
*without* implying such relationships (without writing the template
from scratch, of course)?
TIA for any and all enlightenment you can give, with my best regards,
G. Rodrigues
P.S: Forgive me if my vocabulary is not the most canonical. As I said
I'm new to C++.
P.S: If this is not the right newsgroup to ask these type of
questions, my appologies and could anyone please direct me to a more
apropriate newsgroup?
I'm pretty new to C++ although I have a moderate experience with
programming, especially with Python and, to a lesser extent, with Java
and C.
By necessity I needed to abandon my lovely and flexible Python for...
uh, let's just say the more difficult C++. I've been trying to
understand templates and while I understand the concept I'm not being
able to do anything useful with them.
My problem(s) can be summarized in the folowing simple example:
Suppose you are writing a bunch classes (not necessarily related by
any inheritance relationships) that provide operator== and operator!=
but the latter is always like this
bool <type>:perator!=(const <type>& other) const {
return !(*this == other);
}
It would be nice to have a template that acted like a sort of mixin
and automatically introduced the above source code in the class
definition (and that's what templates are for, source code reuse).
The template is
template<typename T>
class Comparable {
public:
//Assumes operator==.
bool operator!=(const T& other) const {
return !(*this == other);
}
}
And now the class is
class Object {
public:
bool operator==(const Object& other) const {
//Compare by identity.
return this == &other;
}
The leap I'm not being able to make is how to write the Object class
so that the template Comparable gets "mixed in".
A related question: suppose now you are writing a bunch of classes
that also provide the order operators but where all of them are
defined in terms of operator< and operator==, e.g.
bool <type>:perator<=(const <type>& other) const {
return (*this == other) || (*this < other);
};
and similarly for operator> and operator>=. How can I take advantage
of the Comparable template and somehow write a template that inherits
from it? Is it
template<typename T>
class Orderable : public Comparable<T> {
//etc.
}
And assuming the above is correct (that is, it does what I tried to
explain above) does it imply any inheritance relationships for its
intantiations? e.g Orderable<int> is a subclass of Comparable<int>? If
this is the case, is there any possibility of writing the template
*without* implying such relationships (without writing the template
from scratch, of course)?
TIA for any and all enlightenment you can give, with my best regards,
G. Rodrigues
P.S: Forgive me if my vocabulary is not the most canonical. As I said
I'm new to C++.
P.S: If this is not the right newsgroup to ask these type of
questions, my appologies and could anyone please direct me to a more
apropriate newsgroup?