Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

silvermace

Static Members in a Class

This topic is 5838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do i intitialize static members in a class ??
    
//i know this

class A {
  static int answer;
};
int A::answer = 0;

//but this dosnt werk

using namespace std;
class A {
  static vector<int> answers;
};
vector<int> A::answers;     
[edited by - silvermace on July 23, 2002 6:21:57 AM] [edited by - silvermace on July 24, 2002 2:20:31 AM]

Share this post


Link to post
Share on other sites
Advertisement
you''re missing semicolons after class definitions.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites
IIRC you have to do

class A
{
static int a;

A() : a(0) {};
};

*edit* noticed that this does not in fact answer your question.

[edited by - nystagmus on July 23, 2002 7:58:33 AM]

Share this post


Link to post
Share on other sites
And I quote... OK no I dont its too long.

You using VC.Net by any chance? You can only assign static menbers in the class definition in managed C++.

i.e.

class A
{
static double interestrate = 0.05;
}

Regards

BaelWrath

If it is not nailed down it''s mine and if I can prise it loose,
it''s not nailed down!

Share this post


Link to post
Share on other sites
What does "it doesn''t work" mean? The syntax for static initialisation is fine, you should really post the *actual* code which is causing a problem.

Share this post


Link to post
Share on other sites
Of course, Baelwrath is right. You can''t, like I posted earlier, initialize a static member in a constructor, as it would try to re-initialize it for every instance created. My bad.

Share this post


Link to post
Share on other sites
just add two semicolons to the code in your original post.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!