我有一個小問題..我想做汽車註冊,我想擁有所有者(指向一個Person對象的指針),但是當我想帶所有者獲取函數我不能那樣做......這對我來說是神祕......也許我有一點點失誤的地方,我無法找到它。請幫助:)指針tho另一個對象
class Person
{
friend class Cars;
public:
Person(){}
Person(uint8_t age, string name) :m_age(age), m_name(name)
{
m_idGener = s_idGenerator++;
}
int getId()
{
return m_idGener;
}
uint8_t getAge()
{
return m_age;
}
string getName()
{
return m_name;
}
private:
string m_name;
uint8_t m_age;
static int s_idGenerator;
int m_idGener;
};
class Cars:public Person
{
public:
Person *m_owner; // this is my pointer which i want to point to cars Object
it will stay public for the test after the test i will move it in the private section
Cars()
{
}
// getters and setters
Cars setOwner(Cars &object, Person &owner) // with this function i set the owner
{
object.m_owner = &owner;
}
Cars getOwner(Cars &object) /// here is my problem i can't take the owner
{
return *(object.m_owner); // but i can't take the owner
}
uint16_t getHorsePower()
{
return horsePower;
}
string getRegNumber()
{
return registrationNumber;
}
private:
string m_manufacturerName;
string modelName;
uint16_t horsePower;
string registrationNumber;
};
int Person::s_idGenerator = 0;
非常感謝你我是初學者,現在我正在學習代碼:) –
歡迎您。如果它對你有幫助,你可以考慮upvote。 – quantummind