Dies könnte Ihnen helfen:
Vector3.h
#ifndef VECTOR3_H
#define VECTOR3_H
namespace myNamespace { // Add This Because Many Libraries may define a Vector3 Object
class Vector3 {
union {
float m_f3[3];
struct {
float m_fx;
float m_fy;
float m_fz;
};
};
inline Vector3();
inline Vector3(float x, float y, float z);
inline Vector3(float *pfv);
~Vector3();
// Add Your inline overloaded operators & inline function declarations here.
};
#include "Vector3.inl"
} // namespace myNameSpace
#endif // VECTOR3_H
Vector3 .inl
// ------------------------------------------------------
// Vector3() - Default Constructor Sets All Values To 0
inline Vector3::Vector3() :
m_fx(0),
m_fy(0),
m_fz(0) {
} // Vector3
// ------------------------------------------------------
// Vector3() - Takes (x,y,z) Parameters
inline Vector3::Vector3(float x, float y, float z) :
m_fx(x),
m_fy(y),
m_fz(z) {
} // Vector3
// ------------------------------------------------------
// Vector3() - Takes A Pointer To Type
inline Vector3::Vector3(float *pfv) :
m_fx(pfv[0]),
m_fy(pfv[1]),
m_fz(pfv[2]) {
} // Vector3
Vector3.cpp
#include "Vector3.h"
namespace myNamespace {
// ----------------------------------------------
// ~Vector3() - Default Destructor - Does Nothing
// Only Defined Here So That There Is Some Type Of Translation
// Unit In The Object File Generated When Compiling
Vector3::~Vector3() {
} // ~Vector3
} // namespace myNamespace
diese zu benutzen:
main.cpp
#include <iostream>
#include "Vector3.h"
int main() {
using namespace myNamespace; // Forgot to add this line here.
Vector3 v1;
std::cout << "Default Constructor Used: " << std::endl;
std::cout << "v1 = (" << v1.m_fx << ", "
<< v1.m_fy << ", "
<< v1.m_fz << ")" << std::endl;
// Or This Way Works Too
std::cout << "v1 = (" << v1.m_f3[0] << ", "
<< v1.m_f3[1] << ", "
<< v1.m_f3[2] << ")" << std::endl;
Vector3 v2(2.2f, 3.3f. 4.4f);
std::cout << "2nd Constructor Used: " << std::endl;
std::cout << "v2 = (" << v2.m_f3[0] << ", "
<< v2.m_f3[1] << ", "
<< v2.m_f3[2] << ")" << std::endl;
float f3[3] = { 4.5f, 6.7f, 8.9f };
Vector3 v3(f3);
std::cout << "3rd Constructor Used: " << std::endl;
std::cout << "v3 = (" << v3.m_fx << ", "
<< v3.m_f3[1] << ", "
<< v3.m_fz << ")" << std::endl;
} // main
EDIT:
Wenn Sie eine eine Klasse, die enthält Instanzen von ein anderer Benutzer definiert Klasse, dann ist es so einfach:
SomeClass.h
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
namespace myNamespace {
class Vector3;
class SomeClass {
private:
Vector3 m_currentPosition;
Vector3 m_velocity;
Vector3 m_finalPosition;
public:
SomeClass(Vector3 initialPosition, Vector3 currentVelocity);
Vector3 getFinalPosition() const;
private:
void calculateFinalPosition();
};
} // namespace myNamespace
SomeClass.cpp
#include "SomeClass.h"
#include "Vector3.h"
namespace myNamespace {
SomeClass::SomeClass(Vector3 initialPosition, Vector3 currentVelocity) :
m_currentPosition(initialPosition),
m_velocity(currentVelocity) {
calculateFinalPosition();
}
Vector3 SomeClass::getFinalPosition() const {
return m_finalPosition;
} // getFinalPosition
void SomeClass::calculateFinalPosition() {
// Perform The Appropriate Calculation Here To Find The Final Position
} // calculateFinalPosition
} // namespace myNamespace
jedoch für diese zu arbeiten; Dies würde erfordern, dass der Vector3 die überladenen Operatoren, Funktionen wie Kreuz, Punkt usw., definiert.
Wenn Sie alle Membervariablen veröffentlichen, warum brauchen Sie Get/Set-Funktionen? – kfsone
Haben Sie versucht * kompilieren * es? Der Compiler ist sehr gut darin, Syntaxfehler zu finden und zu melden. –
Außerdem kann eine Klasse sehr ähnlich wie jeder andere Typ verwendet werden. Wenn Sie also wissen, wie Sie lokale Variablen eines Klassentyps deklarieren, wissen Sie bereits, wie Sie Membervariablen deklarieren. –