#include <iostream>
usingnamespace std;
class something {
protected:
struct random {
public:
union {
int x;
int v;
};
int a;
};
static random* get;
};
class another : public something {
public:
int test(int value) {
get->x = value;
return get->x;
}
};
int main() {
int ret;
another thing;
cout << "Enter a value: ";
cin >> ret;
cout << thing.test(ret);
}
Well for some reason that didn't work? However, when I changed random from static to non static it worked, why is that? I'll try and define it again outside the class once more but how come it works without static?
That's... very helpful. What do you mean by "didn't work?" If you left the (static) declaration in the class, the definition outside the class should work.
However, when I changed random from static to non static it worked, why is that? I'll try and define it again outside the class once more but how come it works without static?
It's always good to use keywords when you don't know what they do.
A static variable in a class is a "class" variable. It is a single variable that exists as a member of the class. A non-static variable is an "instance" variable where every object or instance of the class type has its own member variable.
@Cire sorry for being extremely vague with my response I sort of have, not really...And yeah I kind of had an understanding of static its just that this was the first for me using them. But thanks for explaining it to me better.
Anyways the problem I have now is how do I set get to point to all the values within random? As this didn't work obviously?
#include <iostream>
usingnamespace std;
class something {
protected:
struct random {
public:
union {
int x;
int v;
};
int a;
};
static random* get;
};
something::random* something::get = something::random;
class another : public something {
public:
int test(int value) {
get->x = value;
return get->x;
}
};
int main() {
int ret;
another thing;
cout << "Enter a value: ";
cin >> ret;
cout << thing.test(ret);
}
#include <iostream>
usingnamespace std;
class something {
protected:
struct random {
public:
union {
int x;
int v;
};
int a;
};
static random* get;
};
something::random* something::get = something::random* something::random;
class another : public something {
public:
int test(int value) {
get->x = value;
return get->x;
}
};
int main() {
int ret;
another thing;
cout << "Enter a value: ";
cin >> ret;
cout << thing.test(ret);
}
Thanks to the furry guy for link, as it helped my understand of static members more!
Oh my goodness this is seriously an issue lol. From now on I pledge to be as specific as possible haha. Ok what I would like to do is make *get be a static pointer that I can use in a derived class in order to be able to use/manipulate the variables within random with the use of the get pointer. And I'm using a union because unions hold only a single value and well I'm only going to be storing and using one object at a time so that's why. And the error is at line 16 when I'm trying to initialize get to the random class that I can use get and have it points to its objects within the derived classes.
Ok what I would like to do is make *get be a static pointer that I can use in a derived class in order to be able to use/manipulate the variables within random with the use of the get pointer.
Why a pointer? What do you mean "within random?" something::random is a type.
And I'm using a union because unions hold only a single value and well I'm only going to be storing and using one object at a time so that's why.
Variables hold single values and you can use one at a time. Why are you using a union?
And the error is at line 16 when I'm trying to initialize get to the random class that I can use get and have it points to its objects within the derived classes.
You have no objects of type something::random, so you have nothing to point your pointer to.
Sorry about my misunderstanding and my poor word choice.
Why a pointer? What do you mean "within random?" something::random is a type.
Well I guess it doesn't have to be a pointer, I could make it not pointer. However what I mean "within random" was have the static value be a "random" type and thus house all of its components is what I meant.
Variables hold single values and you can use one at a time. Why are you using a union?
I know variables hold single and I can use on a time but I'm using a union because they only hold a single object out of a bunch of objects at a time and well yeah, I'll only have one object active/in use at a time. Unless my understanding of unions is incorrect, in which case fail...
You have no objects of type something::random, so you have nothing to point your pointer to.
I understand that but the problem is that how do I assign it to point to something. Where do I even get a value of type random for this thing to point to?
The only thing I know that it can be assigned to is a nullptr, which is just sad...
#include <iostream>
usingnamespace std;
class something {
protected:
struct random {
public:
union {
int x;
int v;
};
int a;
};
static random* get;
};
something::random* something::get = nullptr;
class another : public something {
public:
int test(int value) {
get->x = value;
return get->x;
}
};
int main() {
int ret;
another thing;
cout << "Enter a value: ";
cin >> ret;
cout << thing.test(ret);
}
Here's something that compiles. I'm 99% sure this isn't what you want to do. You still haven't really told us what you're trying to do. You've told us the "solution" you want to use, but we don't know what problem you think you're solving with it.
#include <iostream>
usingnamespace std;
class something {
protected:
struct random {
public:
union {
int x;
int v;
};
int a;
};
static random object;
};
something::random something::object ;
class another : public something {
public:
int test(int value) {
something::object.x = value;
return value;
}
};
int main() {
int ret;
another thing;
cout << "Enter a value: ";
cin >> ret;
cout << thing.test(ret);
}
Ok let me tell you what I'm trying to do, as this was a small example compared to the real thing I was having issue. Anyways the problem I'm using this to solve for is making an armor class. In my armor class I have a struct and inside a union of armor types, steel, iron, leather, etc. Also inside the struct are the states, physical resist, cold resist, fire resist, etc. And then I have 4 static variables of that struct for each one of my classes. The helm class, chest class, leggings class, and gloves class. The reason I needed static variables was so that they wouldn't have to be remade for each class instance. And then within the base class, in the public section I have virtual functions for my derived classes.
Wait can't you change/switch which object you want in a union at run time or no? I'm really inexperienced with them and I was only told or saw that they hold one object at a time. Also I never even thought about using an enum. Are enums/ the variables they hold static, can you make them static?
EDIT: Jesus my grammar/format was terrible in that last post after* re-reading.
EDIT2: Alright you can make enums static although I'd need to learn how to use them correctly.
Wait can't you change/switch which object you want in a union at run time or no?
Yes. But you have to keep track of which member of a union is active. And since you have to keep track of that, you're just introducing another level of (unnecessary) indirection with a union.
Also I never even thought about using an enum. Are enums/ the variables they hold static, can you make them static?
An enum is a type. (You may also want to consider a scoped enum, but I'll let you research that for yourself.) A variable of enum type can be static, yes, if you make it so. (But I think you're barking up the wrong tree with static variables here, too.)
But aren't static variables kinda like global variables with a scope? Thus they never change in values unless you manually change them correct? Thus wouldn't that be useful since I always know what my armor values are?
Alright I'm going to make another pledge in that I will now correctly format and revise my sentences haha.
Thus wouldn't that be useful since I always know what my armor values are?
What I meant to say was wouldn't it be useful to have my variables mainly since they can always store armor stats, thus I always know what armor I have and they won't be replaced unless they need to be.
What I meant to say was wouldn't it be useful to have my variables mainly since they can always store armor stats, thus I always know what armor I have and they won't be replaced unless they need to be.
I still don't know what you're trying to say. But, consider - would you ever want more than one player in the game? Would you ever want your player to control more than one character in a party?
Would you ever want to reuse the armor class for npcs?
Do you imagine having these static variables would help or hinder in these hypothetical situations?
Do you think this one is a better version than the other one I posted earlier? I changed the union to an enum struct(which is a scoped enum correct?) to make more simple. I also put all the body part pieces into a single struct and then had a static variables of that struct type to permanently house/know the variable info.