C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 24th, 2003, 05:06 AM
majelbstoat's Avatar
majelbstoat majelbstoat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Manchester, England
Posts: 39 majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 h 4 m 46 sec
Reputation Power: 16
Using static modifier with template classes

Hi,

Does anyone have experience of the following? The member attribute counter is declared static so I can keep track of how many variables are in existence. However, this means that it isn't actually defined yet, right?

To do that I have to declare it outside of the class. If this much is true, then the problem I'm having is getting the syntax right on the line that I've commented out, because things are confused by the template <> stuff.

Quite new to this part of C++. Any help much appreciated!

Code:
// Class to store variable information.
template <class PType> class variable
{
  // Counter to ensure maximum limit of variables not breached.
  static int counter;
public:

  // Variable name and value.
  char *name;
  PType value;

  // Constructors.
  variable(void);
  variable(char *n, PType v);
};
//int variable<PType>::counter;

Reply With Quote
  #2  
Old April 24th, 2003, 05:52 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
I looked it up, but it's too complicated. Search google for:

"Static members of template classes"

Here's an example I found:
Code:
template <class T> class key
{
public:
      static T x;
};
template <class T> T key<T> ::x; // template definition
void main()
{
      key<int>::x = 0;
}


Based on that, I think this might work:
Code:
template <class PType> class variable
{
  static int counter;
public:

  // Variable name and value.
  char *name;
  PType value;

  // Constructors.
  variable(void);
  variable(char *n, PType v);
};

template<class PType> int variable<PType>::counter;

int main()
{
    variable<double>::counter=0;
...
...
...
}

Last edited by 7stud : April 24th, 2003 at 06:38 AM.

Reply With Quote
  #3  
Old April 25th, 2003, 11:15 AM
majelbstoat's Avatar
majelbstoat majelbstoat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Manchester, England
Posts: 39 majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level)majelbstoat User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 4 h 4 m 46 sec
Reputation Power: 16
Magic, worked first time!

Thanks 7stud!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Using static modifier with template classes

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap