Jump to content
  • Advertisement
Sign in to follow this  
Pelikaruga

Assigning a valua to a static variable in a class without creating an object.

This topic is 4832 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

I am programming a library to use in my games and would like to be able to set the value of a static pointer I have in one of my classes without creating an object of that class to do it. If this is not possible, I guess I have to use a dummy class in the initialization phase for it. Thanks in advance! EDIT: Also, is it possible to have a value in a class that can be read like it would be a public value, but can't be accessed from outside the object like it would be a private value?

Share this post


Link to post
Share on other sites
Advertisement
If it's a static pointer then it's already not associated with an object


struct Foo
{
static int* ptr;
};

int* Foo::ptr = 0;

/* ... */

Foo::ptr = new int[128];

/* ... */

delete[] Foo::ptr;


See, no Foo objects are involved at all.

Share this post


Link to post
Share on other sites
What I want to do is share a pointer for all objects of a class, like I would do with an integer.


class Foo
{
public:
static int var;
}

Foo foo1;
Foo foo2;

foo1.var = 1;

cout << foo2.var; // this should result in printing 1 on the screen


Can't I do this with a pointer?

Share this post


Link to post
Share on other sites
I am getting a link error when I declare the pointer as static. There is no problem when I don't declare it as static.

"error LNK2001: unresolved external symbol"

I guess I am just doing a stupid common mistake here.

Share this post


Link to post
Share on other sites
Quote:
Also, is it possible to have a value in a class that can be read like it would be a public value, but can't be accessed from outside the object like it would be a private value?


Read/write within the class, but read-only for the rest of the code? If you want it to look like a value, there are ways, but nothing you'd enjoy right now. Otherwise, the simplest way is to provide a member function that returns the value:

class Foo
{
private:
int m_value;
public:
const int& value() const { return m_value; }
};




Quote:
foo1.var = 1;


Static members are not accessed through an object. You have to do Foo::var, not foo1.var or foo2.var.

Howver, you can, again, add a member function that will access the static member for you.

class Foo
{
private:
static int s_value;
public:
const int& value() const { return s_value; }
Foo& value(int v) { s_value = v; return *this; }
};

int Foo::s_value;

Foo f1,f2;
f1.value(5);
std::cout << f2.value() << std::endl;




Additional work can be done to make it look more a value. Ask again if you really want to know how.

Quote:
I am getting a link error when I declare the pointer as static. There is no problem when I don't declare it as static.

"error LNK2001: unresolved external symbol"

I guess I am just doing a stupid common mistake here.


Indeed. Notice how I've always added a line like "int* Foo::ptr = 0;" or "int Foo::s_value;". The static declaration in the class body does not define the variable. You still have to do that separately (and not in the header file, otherwise you'll get complaints about multiple definitions).

Share this post


Link to post
Share on other sites
Ah, yes, thank you very much. I am a bit rusty after a few months without being able to program :)

Thanks for the help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!