Best way to use a derived member as a template parameter to a base

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Champ

    Best way to use a derived member as a template parameter to a base

    What I want to do (and can't...) is

    Template <typename Tclass base
    {
    // whatever
    }

    class derived: public base<derived::m yVals>
    {
    Enum myVals
    {
    // this isn't relevant either
    }
    }

    ....so that I can have a function in the base that takes the enum as a
    parameter.

    At the moment I'm running with a helper class that contains the enum
    (and nothing else); but it's ugly, because it separates the enum from
    its proper class. What do people think is the best way around this?

    Ta
    Andy
  • Victor Bazarov

    #2
    Re: Best way to use a derived member as a template parameter to abase

    Andy Champ wrote:
    What I want to do (and can't...) is
    >
    Template <typename Tclass base
    {
    // whatever
    }
    >
    class derived: public base<derived::m yVals>
    {
    Enum myVals
    {
    // this isn't relevant either
    }
    }
    >
    ...so that I can have a function in the base that takes the enum as a
    parameter.
    >
    At the moment I'm running with a helper class that contains the enum
    (and nothing else); but it's ugly, because it separates the enum from
    its proper class. What do people think is the best way around this?
    No way around this. You cannot use a member of the class *before*
    defining the class itself (and the member).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • mlimber

      #3
      Re: Best way to use a derived member as a template parameter to abase

      On Aug 14, 3:28 pm, Andy Champ <no....@nospam. comwrote:
      What I want to do (and can't...) is
      >
      Template <typename Tclass base
      {
      // whatever
      >
      }
      >
      class derived: public base<derived::m yVals>
      {
      Enum myVals
      {
      // this isn't relevant either
      }
      >
      }
      >
      ...so that I can have a function in the base that takes the enum as a
      parameter.
      >
      At the moment I'm running with a helper class that contains the enum
      (and nothing else); but it's ugly, because it separates the enum from
      its proper class. What do people think is the best way around this?
      Use a namespace to group the enum and the derived class.

      Cheers! --M

      Comment

      • Tonni Tielens

        #4
        Re: Best way to use a derived member as a template parameter to abase

        On Aug 14, 9:28 pm, Andy Champ <no....@nospam. comwrote:
        What I want to do (and can't...) is
        >
        Template <typename Tclass base
        {
        // whatever
        >
        }
        >
        class derived: public base<derived::m yVals>
        {
                Enum myVals
                {
                        // this isn't relevant either
                }
        >
        }
        >
        ...so that I can have a function in the base that takes the enum as a
        parameter.
        >
        At the moment I'm running with a helper class that contains the enum
        (and nothing else);  but it's ugly, because it separates the enum from
        its proper class.  What do people think is the best way around this?
        One other way to do this is to put the enum in a separate wrapper
        class before you define your Derived class. Something like:

        struct MyValues
        {
        enum MyValuesEnum
        {
        // Your values here.
        };
        };

        class Derived : public Base<MyValues:: MyValuesEnum>
        {
        // You're class members here.
        };

        Good luck.

        Comment

        • Andy Champ

          #5
          Re: Best way to use a derived member as a template parameter to abase

          mlimber wrote:
          >
          Use a namespace to group the enum and the derived class.
          >
          Cheers! --M
          I've nearly taken your advice. Something like this:

          namespace espace
          {
          enum base { base1, base2, etc };
          }

          template <typename Tclass base
          {
          public:
          void someFunc(T param);
          ....
          }

          namespace espace
          {
          enum derived1
          {
          value1 = base1,
          value2 = 1000 /* to keep it well clear of base */
          etc
          }
          }

          class derived1: public base<espace::de rived1>
          {
          ....
          }

          namespace espace
          {
          enum derived2
          {
          value1 = base2,
          value2 = 1000 /* to keep it well clear of base */
          etc
          }
          }

          class derived2 public base<espace::de rived2>
          {
          ....
          }

          I can use derived1::someF unc with espace::derived 1 and
          derived2::someF unc with espace::derived 2, and it type checks - so this
          is good enough.

          Thanks guys

          Andy

          Comment

          Working...