我是 C++ 新手。请看下面的代码
位置.h
#pragma once
class Position
{
public:
Position(int x, int y, int z);
Position();
~Position();
void display();
private:
int x;
int y;
int z;
};
位置.cpp
#include "Position.h"
#include <iostream>
using namespace std;
Position::Position(int x, int y, int z)
{
this->x = x;
this->y = y;
this->z = z;
}
Position::Position(){}
Position::~Position(void)
{
}
游戏组件.h
#pragma once
#include<time.h>
class GameComponent
{
public:
GameComponent(int);
GameComponent();
~GameComponent(void);
virtual void update(const tm*);
friend class Position;
private:
int id;
};
游戏组件.cpp
#include "GameComponent.h"
#include <iostream>
#include <time.h>
#include "DrawableGameComponent.h"
using namespace std;
GameComponent::GameComponent(int v):id(v)
{
}
GameComponent::GameComponent(){}
GameComponent::~GameComponent(void)
{
}
void GameComponent::update(const tm* time)
{
cout << "ID : " << id << endl;
cout << "Update: " << time->tm_hour << ":" << time->tm_min << ":" << time->tm_sec << endl;
//Position::displayPositions();
}
void Position::display()
{
cout << "X" << x << "Y" << y << "Z" << z << endl;
}
我需要访问GameComponent方法display()
中属于类的朋友类Position
的方法。实际上我想做的是访问 class 的私有数据成员。我怎样才能做到这一点?请帮忙GameComponent
update(const tm* time)
Position