Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template classes with static members

Reply
Thread Tools

Template classes with static members

 
 
Jonathan Lee
Guest
Posts: n/a
 
      07-29-2010
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-29-2010
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
 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      07-29-2010
On Jul 29, 4:36*pm, Victor Bazarov <v.baza...@comcast.invalid> 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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-30-2010
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why can nsmc, local classes or anonymous classes have static members? Rit Java 23 01-03-2010 05:54 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
static const members of template classes? Steven T. Hatton C++ 3 10-01-2004 08:06 AM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM
static members of template classes Alibek C++ 3 12-17-2003 06:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57