Struct inside a Class

May 9, 2020 at 1:07am
I am trying to use a public class method to change or set the variables inside of a struct inside of the class. what syntax would I use?

1
2
3
4
5
6
7
8
9
10
11
12
13
class myClass
{
    public:
    void setStructVal(int varToUse, int valToSet)
    {
        myClass::myStruct.value = valToSet;
    }
    private:
    struct myStruct
    {
        int value;
    }
}
May 9, 2020 at 1:12am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class myClass
{
public:
    void setStructVal(int varToUse, int valToSet)
    {
        data.value = valToSet;
    }

private:
    struct myStruct
    {
        int value;
    };
    myStruct data;
};
Last edited on May 10, 2020 at 8:38am
May 9, 2020 at 1:13am
You to instantiate an instance of your myStruct, like any other non-static member of a class/struct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
class myClass
{
    public:
    void setStructVal(int varToUse, int valToSet)
    {
        struct_instance.value = valToSet;
    }
    private:
    struct myStruct
    {
        int value;
    } struct_instance;
};

int main()
{
    myClass octopus;
    octopus.setStructVal(0xBAD, 42);
}

PS: You never use varToUse, ironically.
Last edited on May 9, 2020 at 1:13am
May 9, 2020 at 1:15am
Well you would first need an object of myStruct

1
2
3
4
5
6
7
8
9
10
class myClass
{
public:
    void setStructVal(int valToSet)  {str.value = valToSet;}
private:
    struct myStruct
    {
        int value;
    } str;
};

Although I don't see why this is necessary in your case. Just make value a private data member of myClass.

You never use varToUse, ironically.

Lol. This made me chuckle
Last edited on May 9, 2020 at 1:17am
May 9, 2020 at 5:12pm
My thoughts where that if I have a class and I want seperated containers in that class that hold private variables I could use a struct to contain the variables.

What would be a better way to accomplish this?
May 9, 2020 at 6:16pm
Declaring a struct in a class, like declaring a struct anywhere else, just declares a type. It's not an object in itself and must be instantiated before it exists as an object.

If the struct is useful as an organizing type inside the class then there is a good chance it will be useful outside the class. In particular, it may be useful to return or accept a copy or reference to the struct.

1
2
3
4
5
6
7
8
9
struct Point {
    int x, y;
};

class Animal {
    Point position;
public:
    Point getpos() const { return position; }
};

Private structs are an implementation detail and there should generally be an actual need for a struct.

1
2
3
4
5
6
7
8
9
10
class List {
    struct Node {
        Node *next;
        int   data;
    };
    Node*  root = nullptr;
    size_t size = 0;
public:
    //...
};

You need to give more detail to determine what's right for your situation.
May 9, 2020 at 6:51pm
It is for organization.

1
2
3
4
5
6
7
8
9
10
11
12
13
class charSheet {
    struct stats {
    int stat01;
    int stat02;
    }stats;
}
    struct skills {
    int skill01;
    int skill02;
    }
// to call or set
charSheet player01;
player01.stat.stat01 = 4;
May 9, 2020 at 7:39pm
Why not use arrays. It's more extensible and programmatic.

1
2
3
4
5
6
7
class CharSheet {
    int stat[2];
    int skill[2];
};

CharSheet player[2];
player[0].stat[0] = 4;

Last edited on May 9, 2020 at 7:40pm
May 9, 2020 at 9:17pm
You could make the variables inside the struct static. Then you wouldn't need an object.

1
2
3
4
5
6
7
8
9
10
11
12
13
class myClass
{
    public:
    void setStructVal(int varToUse, int valToSet)
    {
        myStruct::value = valToSet;
    }
    private:
    struct myStruct
    {
        static inline int value = 0;
    };
};
May 10, 2020 at 4:01pm
An array would mean I would need to know what skill [1] was. With a struct I can call them directly by name more intuitively. The one thing I don't know why is: see below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class charSheet {
    struct stats {//<--- What is "stats" function here? I am just declaring the type?
    int stat01;
    int stat02;
    }stats;//<----Why do I need to do this?? Initializing the struct for access?

    struct skills {
    int skill01;
    int skill02;
    }skills;//<----Why do I need to do this??
// to call or set
int main()
{
charSheet player01;
player01.stats.stat01 = 4;
cout << player01.stats.stat01 << endl;
}
	
May 10, 2020 at 4:07pm
Why do you need to create an instance of the struct contained in your class? For the same reason why you need to instantiate an object of your class in main.

Hard to work with an object that doesn't exist.
May 10, 2020 at 4:52pm
Now I know, and knowing is half the battle. YO JOE!
May 10, 2020 at 5:01pm
You don't need to define the struct within the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct myStruct
{
   int value;
};

class myClass
{
public:
   void setStructVal(int varToUse, int valToSet)
   {
      struct_instance.value = valToSet;
   }
private:
   myStruct struct_instance;
};
Topic archived. No new replies allowed.