使用另一个 class 的变量
Using variable of another class
我想用 classes.
制作某种层次结构
示例:
object.position.x
我希望能够将 Vector3 class 重新用于速度和其他方面,这就是为什么我想将其保留为 class。
我那样试过。
#include"Vector3.h"
class object{
public:
Vector3 position;
Vector3 velocity;
}
-------------------------------------
class Vector3{
float x;
float y;
float z;
}
但它似乎没有我预期的那么好用。
当我选择 go.position.x = 0
代码块时,我要使用 ->
。
request for member 'x' in 'go.GameObject::position', which is of pointer type 'Vector3*' (maybe you meant to use '->' ?)|
但是当我这样使用它们时:go.position -> x = 0
,程序崩溃了。
有人可以告诉我 "right" 或更好的方法是什么吗?
由于 class 的成员在默认情况下是私有的,因此您无法像 position.x 那样访问它们。你需要为他们写 getter 和 setter ,否则让它像 .
一样结构
structVector3{
float x;
float y;
float z;
};
您粘贴的代码不完整,您收到的错误似乎来自不同版本的代码。下面是使用 Your 类 和不带指针的示例。
#include <iostream>
using namespace std;
class Vector3{
public:
float x;
float y;
float z;
};
class Object {
public:
Vector3 position;
Vector3 velocity;
};
class ObjectPointer {
public:
Vector3 *positionPtr;
Vector3 *velocityPtr;
ObjectPointer()
: positionPtr(new Vector3()),
velocityPtr(new Vector3())
{
}
};
int main() {
// your code goes here
Object obj;
obj.position.x = 1;
obj.position.y = 2;
obj.position.z = 3;
obj.velocity.x = 11;
obj.velocity.y = 12;
obj.velocity.z = 13;
std::cout << "Object Position (" << obj.position.x
<< ", " << obj.position.y
<< ", " << obj.position.z << ")" << std::endl;
std::cout << "Object Velocity (" << obj.velocity.x
<< ", " << obj.velocity.y
<< ", " << obj.velocity.z << ")" << std::endl;
ObjectPointer *objPtr = new ObjectPointer();
objPtr->positionPtr->x = 5;
objPtr->positionPtr->y = 6;
objPtr->positionPtr->z = 7;
objPtr->velocityPtr->x = 15;
objPtr->velocityPtr->y = 16;
objPtr->velocityPtr->z = 17;
std::cout << "ObjectPointer Position (" << objPtr->positionPtr->x
<< ", " << objPtr->positionPtr->y
<< ", " << objPtr->positionPtr->z << ")" << std::endl;
std::cout << "ObjectPointer Velocity (" << objPtr->velocityPtr->x
<< ", " << objPtr->velocityPtr->y
<< ", " << objPtr->velocityPtr->z << ")" << std::endl;
return 0;
}
我想用 classes.
示例:
object.position.x
我希望能够将 Vector3 class 重新用于速度和其他方面,这就是为什么我想将其保留为 class。
我那样试过。
#include"Vector3.h"
class object{
public:
Vector3 position;
Vector3 velocity;
}
-------------------------------------
class Vector3{
float x;
float y;
float z;
}
但它似乎没有我预期的那么好用。
当我选择 go.position.x = 0
代码块时,我要使用 ->
。
request for member 'x' in 'go.GameObject::position', which is of pointer type 'Vector3*' (maybe you meant to use '->' ?)|
但是当我这样使用它们时:go.position -> x = 0
,程序崩溃了。
有人可以告诉我 "right" 或更好的方法是什么吗?
由于 class 的成员在默认情况下是私有的,因此您无法像 position.x 那样访问它们。你需要为他们写 getter 和 setter ,否则让它像 .
一样结构structVector3{
float x;
float y;
float z;
};
您粘贴的代码不完整,您收到的错误似乎来自不同版本的代码。下面是使用 Your 类 和不带指针的示例。
#include <iostream>
using namespace std;
class Vector3{
public:
float x;
float y;
float z;
};
class Object {
public:
Vector3 position;
Vector3 velocity;
};
class ObjectPointer {
public:
Vector3 *positionPtr;
Vector3 *velocityPtr;
ObjectPointer()
: positionPtr(new Vector3()),
velocityPtr(new Vector3())
{
}
};
int main() {
// your code goes here
Object obj;
obj.position.x = 1;
obj.position.y = 2;
obj.position.z = 3;
obj.velocity.x = 11;
obj.velocity.y = 12;
obj.velocity.z = 13;
std::cout << "Object Position (" << obj.position.x
<< ", " << obj.position.y
<< ", " << obj.position.z << ")" << std::endl;
std::cout << "Object Velocity (" << obj.velocity.x
<< ", " << obj.velocity.y
<< ", " << obj.velocity.z << ")" << std::endl;
ObjectPointer *objPtr = new ObjectPointer();
objPtr->positionPtr->x = 5;
objPtr->positionPtr->y = 6;
objPtr->positionPtr->z = 7;
objPtr->velocityPtr->x = 15;
objPtr->velocityPtr->y = 16;
objPtr->velocityPtr->z = 17;
std::cout << "ObjectPointer Position (" << objPtr->positionPtr->x
<< ", " << objPtr->positionPtr->y
<< ", " << objPtr->positionPtr->z << ")" << std::endl;
std::cout << "ObjectPointer Velocity (" << objPtr->velocityPtr->x
<< ", " << objPtr->velocityPtr->y
<< ", " << objPtr->velocityPtr->z << ")" << std::endl;
return 0;
}