C++ templates
-
I have class A and class B:
template<typename T>
struct A
{
T data;
...
};template<typename T>
struct B
{
T data;
...
};I can do the following:
B<A<int> > b1;
B<A<MyOtherType> > b2;But, these lines are so ugly and I want to make special type. I want to have something like this:
myBA<int> b1;
myBA<MyOtherType> b2;How I can do it ? or where I can read about templates like these ?
-
I have class A and class B:
template<typename T>
struct A
{
T data;
...
};template<typename T>
struct B
{
T data;
...
};I can do the following:
B<A<int> > b1;
B<A<MyOtherType> > b2;But, these lines are so ugly and I want to make special type. I want to have something like this:
myBA<int> b1;
myBA<MyOtherType> b2;How I can do it ? or where I can read about templates like these ?
-
I have class A and class B:
template<typename T>
struct A
{
T data;
...
};template<typename T>
struct B
{
T data;
...
};I can do the following:
B<A<int> > b1;
B<A<MyOtherType> > b2;But, these lines are so ugly and I want to make special type. I want to have something like this:
myBA<int> b1;
myBA<MyOtherType> b2;How I can do it ? or where I can read about templates like these ?
template<typename T>
struct myBA : public B< A<T> >
{
};...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack
-
template<typename T>
struct myBA : public B< A<T> >
{
};...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack
Yes, I know that approach. But I wanna have another way, without declaration of new class.
-
Yes, I know that approach. But I wanna have another way, without declaration of new class.
You can write it as one line of code, do you expect to find something shorter ? What's wrong with declaring a new class? That's what you asked for.
...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack
-
You can write it as one line of code, do you expect to find something shorter ? What's wrong with declaring a new class? That's what you asked for.
...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack
Thanks. Could you recommend me any books when there is detailed descriptions of these questions ?
-
I have class A and class B:
template<typename T>
struct A
{
T data;
...
};template<typename T>
struct B
{
T data;
...
};I can do the following:
B<A<int> > b1;
B<A<MyOtherType> > b2;But, these lines are so ugly and I want to make special type. I want to have something like this:
myBA<int> b1;
myBA<MyOtherType> b2;How I can do it ? or where I can read about templates like these ?
Here is one way to do it.
typedef A<int> IntA;
typedef A<MyOtherType> MyOtherInt;B<IntA> b1;
B<MyOtherInt> b2;Or you could go one step further.
typedef A<int> IntA;
typedef A<MyOtherType> MyOtherInt;typedef B<IntA> myIntBA;
typedef B<MyOtherInt> myOtherBA;myIntBA b1;
myOtherBA b2;You could also make the above into a single typedef.
typedef B<A<int>> myIntBA;
typedef B<A<MyOtherType>> myOtherBA;myIntBA b1;
myOtherBA b2;«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)