Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ templates

C++ templates

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cpp_fanatic
    wrote on last edited by
    #1

    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 ?

    A C _ 3 Replies Last reply
    0
    • C cpp_fanatic

      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 ?

      A Offline
      A Offline
      amatecki
      wrote on last edited by
      #2

      what is the "myBA" ?

      1 Reply Last reply
      0
      • C cpp_fanatic

        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 ?

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • C cmk

          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

          C Offline
          C Offline
          cpp_fanatic
          wrote on last edited by
          #4

          Yes, I know that approach. But I wanna have another way, without declaration of new class.

          C 1 Reply Last reply
          0
          • C cpp_fanatic

            Yes, I know that approach. But I wanna have another way, without declaration of new class.

            C Offline
            C Offline
            cmk
            wrote on last edited by
            #5

            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

            C 1 Reply Last reply
            0
            • C cmk

              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

              C Offline
              C Offline
              cpp_fanatic
              wrote on last edited by
              #6

              Thanks. Could you recommend me any books when there is detailed descriptions of these questions ?

              1 Reply Last reply
              0
              • C cpp_fanatic

                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 ?

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                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++)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups