Adding more info to the class outside it?

Let's say I have this class:
1
2
3
4
5
class myClass {
    int x,y;
    public:
    int xx,yy;
    }

How to add more info from somewhere else?

I have tried this:
1
2
3
4
5
6
7
8
9
class myClass {
    int x,y;
    public:
    int xx,yy;
    } myInstance;

int main (){
    myInstance.xxx=9;
    }

But it says, xxx is undeclared.

How to do it?
I know it's a stupid question, but I'm stuck
If you want to do that without using setters, then make the variables public. Though normally you use a settor or constructor.
You can't add members to the class after it has been defined.
If you want to do that without using setters, then make the variables public. Though normally you use a settor or constructor.

And that means... what?

EDIT: ninja'd
You can't add members to the class after it has been defined.

D:
darn.
Last edited on
If you want to simulate class members, use a map<string, type>
myClass is the cookie cutter, and myInstance is a cookie.
Everything that myInstance needs to have, myClass must have by design.

Edit:
Also
And that means... what?

LB is referring to how you need a function for setting (a "setter") a value that is not public (in your case x and y).

Just try this to see: myInstance.x = 555;
Last edited on
Is it a good idea to create a big array in a class and store variables there?
Like this:
1
2
3
4
5
6
7
8
9
10
11
class myClass {
    int x,y;
    public:
    int array[32000];
    } myInstance;

int main(){
    myInstance.array[0]=0; //xx
    myInstance.array[1]=0; //yy
    }


btw thanks for explaining that getter/setter thing that LB said.
Last edited on
It's not a good idea, that is a HUGE waste of memory, not to mention a fast ticket to a stack overflow.
Topic archived. No new replies allowed.