Archived

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

Static Members in a Class

This topic is 5623 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
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
quote:
Original post by silvermace
ok, all i wanna know is how the hell do i intialize a vector ???
thanx

Your code should run the default ctor for the vector. You need to explain more about your problem if that is not your idea of initializing a vector.

Share this post


Link to post
Share on other sites
its quite simple if you know C++, which i am amuming you do...
static member variabls in a class ARE SHARED BY ALL INSTANCES OF THAT CLASS. ok.
to allow this, one must Initialize this static variable by setting a value or operating on the variable in the classes scope
ie. A::answers.

how do i assign a value to a vector !!!?!

[edited by - silvermace on July 24, 2002 6:05:11 AM]

Share this post


Link to post
Share on other sites

  
//If you do this:


int A::answer = 0;

// Why don't you try this equally?

vector<int> A::answers = vector<int>(100); // chooce your params...


I tried quickly, it compiled!


[edited by - stefu on July 24, 2002 6:07:27 AM]

Share this post


Link to post
Share on other sites
the default consutructor initializes your vector. you can use constructors with arguments if you want to fill the vector with values on initialization. to get a blank vector, you don''t need to do anything, as the default constructor takes care of init.

---
Come to #directxdev IRC channel on AfterNET

Share this post


Link to post
Share on other sites
niyaw, no offence intended at all, but ur so n00b.
thanx stefu, that sorta helped
can a vector only be assigned a vector ??
or say just an int like 0 ???

  
vector<int> A::answers = 0;

Share this post


Link to post
Share on other sites

  
// int to int

int A::answer = 0;

// char * to char *

char * A::answer = "Hello, world!";

// vector<int> to vector<int>

vector<int> A::answer = vector<int>(0);

// what do you try to achieve by this?

// vector<int> is not a pointer. (I think so)

vector<int> A::answers = 0;



[edited by - stefu on July 24, 2002 6:16:35 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by silvermace
its quite simple if you know C++, which i am amuming you do...

static member variabls in a class ARE SHARED BY ALL INSTANCES OF THAT CLASS. ok.


It''s simple enough to know how to initialize a vector as static member. What''s not simple, and what you''re not explaining, is to know what you are expecting it to do. Getting snotty about it is not going to get you anywhere.
quote:

how do i assign a value to a vector !!!?!

That depends. Since you won''t tell us what you want to initialize it with, here''s some options. stefu''s example of:


  
vector<int> A::answer = vector<int>(0);


is almost certainly not what you want, as this initializes answers with 0 copies of a default constructed int - the same as what the default ctor does. I can think of three options for what you probably want:

Example 1:

  
#include <vector>

using namespace std;

int ar[] = {1,2,3,4,5};
class C
{
static vector<int> answers;
};
// initialize with contents of another collection:

vector<int> C::answers(ar, ar+4);


Example 2:

  
#include <vector>

using namespace std;

class C
{
public:
C()
{
// add values during construction:

initialize();
}

void initialize()
{
if(!initialized_)
{
answers.push_back(1);
answers.push_back(2);

initialized_ = true;
}
}
private:
static vector<int> answers;
static bool initialized_;
};
vector<int> C::answers;
bool C::initialized_ = false;


Alternately, wrap the vector in it''s own class, provide a ctor which performs the necessary initialization, and then make that class a static member. That would be my preferred option.

Share this post


Link to post
Share on other sites