我知道有很多关于C++友元类的问题。然而,我的问题与特定情景有关。在给出的代码中,使用友元类是否合适?
class Software
{
friend class SoftwareProducer;
SoftwareProducer* m_producer;
int m_key;
// Only producers can produce software
Software(SoftwareProducer* producer) : m_producer(producer) { }
public:
void buy()
{
m_key = m_producer->next_key();
}
};
class SoftwareProducer
{
friend class Software;
public:
Software* produce()
{
return new Software(this);
}
private:
// Only software from this producer can get a valid key for registration
int next_key()
{
return ...;
}
};
感谢您的来信,
此致
敬礼!
最好的问候,