Template classes with static members

Discussion in 'C++' started by Jonathan Lee, Jul 29, 2010.

  1. Jonathan Lee

    Jonathan Lee Guest

    Hi all,
    I have a couple situations where I've written a template class that
    has static data or static functions that are the exact same no matter
    what the template type is. So I've been pulling the code out into a
    dummy namespace, or a dummy class that the template inherits from
    because I don't want the code duplicated. But *then* I figure this
    stuff doesn't belong in a header so I move that stuff to a .cpp file,
    which destroys one of the things I like about templates: everything
    in one header.

    Is there a better way to:
    - avoid code/data duplication
    - hide these functions from the outside world
    - ideally keep it all in one header

    --Jonathan
     
    Jonathan Lee, Jul 29, 2010
    #1
    1. Advertising

  2. On 7/29/2010 12:53 PM, Jonathan Lee wrote:
    > Hi all,
    > I have a couple situations where I've written a template class that
    > has static data or static functions that are the exact same no matter
    > what the template type is.


    Looks like a candidate for a base class (or class template).

    > So I've been pulling the code out into a
    > dummy namespace, or a dummy class that the template inherits from
    > because I don't want the code duplicated.


    Not sure why you designate them "dummy", but OK.

    > But *then* I figure this
    > stuff doesn't belong in a header so I move that stuff to a .cpp file,
    > which destroys one of the things I like about templates: everything
    > in one header.


    Uh... You *figure* "this stuff doesn't belong in a header" - how do you
    figure that? If you like everything in one header, then keep it there, no?

    > Is there a better way to:
    > - avoid code/data duplication
    > - hide these functions from the outside world
    > - ideally keep it all in one header


    Uh... The last two requirements are conflicting, don't you think?

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 29, 2010
    #2
    1. Advertising

  3. Jonathan Lee

    Jonathan Lee Guest

    On Jul 29, 4:36 pm, Victor Bazarov <> wrote:
    > On 7/29/2010 12:53 PM, Jonathan Lee wrote:
    > > But *then* I figure this
    > > stuff doesn't belong in a header so I move that stuff to a .cpp file,
    > > which destroys one of the things I like about templates: everything
    > > in one header.

    >
    > Uh...  You *figure* "this stuff doesn't belong in a header" - how do you
    > figure that?  If you like everything in one header, then keep it there, no?


    If they were trivial functions I would inline them and throw
    them in the header. But when they're not... it just seems
    contrary to practice to do that. I guess it's a case of having
    your cake and eating it, too.

    > >    Is there a better way to:
    > >      - avoid code/data duplication
    > >      - hide these functions from the outside world
    > >      - ideally keep it all in one header

    >
    > Uh...  The last two requirements are conflicting, don't you think?


    It sounds like it. But a common base class with protected
    members basically satisfies both. I guess that's about the
    cleanest approach.

    Just the "inline"-ing bothers me.

    --Jonathan
     
    Jonathan Lee, Jul 29, 2010
    #3
  4. Jonathan Lee <> wrote:
    > Is there a better way to:
    > - avoid code/data duplication
    > - hide these functions from the outside world
    > - ideally keep it all in one header


    You could always make the static member private in the base class and
    then declare the derived classes as friends.
     
    Juha Nieminen, Jul 30, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben
    Replies:
    3
    Views:
    445
  2. JFCM
    Replies:
    4
    Views:
    5,767
  3. Alibek
    Replies:
    3
    Views:
    563
    Gianni Mariani
    Dec 17, 2003
  4. Steven T. Hatton

    static const members of template classes?

    Steven T. Hatton, Oct 1, 2004, in forum: C++
    Replies:
    3
    Views:
    3,636
    John Harrison
    Oct 1, 2004
  5. Rit
    Replies:
    23
    Views:
    971
    Mike Schilling
    Jan 3, 2010
Loading...

Share This Page