How to reach a Class within another class or struct?

Hi
I was wondering how to reach a class after I declared it as a example.

So if I ware to do it like


SimpleCube Cube;

how would I reach Position?
via my "Cube" and not via SimpleCube.
Like
Cube.Position.X
and NOT
SimpleCube.Position.X

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class SimpleCube
{

	public:
		SimpleCube();
		void Update();
		class Position
		{
			float x,y;
		
		};
		struct Vertex	//Overloaded Vertex Structure
		{
			Vertex(){}
			Vertex(float x, float y, float z,float r, float g, float b , float a)
				: pos(x,y,z), color(r,g,b,a){}

			XMFLOAT3 pos;
			XMFLOAT4 color;
		};

		float width;

};
Would be much easier to declare Position and Vertex structures separately and give an instance of each to your cube.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct Position
{
  float x, y;
};

struct Vertex
{
  Vertex(){}
  Vertex(float x, float y, float z,float r, float g, float b , float a): pos(x,y,z), color(r,g,b,a){}

  XMFLOAT3 pos;
  XMFLOAT4 color;
};

class SimpleCube
{
private:
  Position pos;
  Vertex vert;
  float width;
public:
  SimpleCube();
  void Update();
};

// Instance
SimpleCube cube;
cube.pos.x = 0;
cube.pos.y = 10;


There's some privacy issues here and there with that example but hopefully you'll catch my drift.
Last edited on
You would need to declare a member variable of type Position in your SimpleCube class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class SimpleCube
{

	public:
		SimpleCube();
		void Update();
		class Position
		{
			float x,y;
		
		};
		struct Vertex	//Overloaded Vertex Structure
		{
			Vertex(){}
			Vertex(float x, float y, float z,float r, float g, float b , float a)
				: pos(x,y,z), color(r,g,b,a){}

			XMFLOAT3 pos;
			XMFLOAT4 color;
		};

		float width;
		Position position;
};
Your class SimpleCube has no a data member of type Position. It has only the definition of type Position. If you want to have a data member of type Position you should declare it in the class.
Ah thanks guys, didnt know you had to do it that way >.<
Topic archived. No new replies allowed.