gpt4 book ai didi

c++ - 嵌套类的替代方法

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:58 24 4
gpt4 key购买 nike

我有两个类 A,B,我只想从类 A 创建类 B 的对象。我不希望其他类创建 B 类的对象。这是代码片段。有什么建议我如何在没有嵌套类的情况下实现?有人可以建议解决这个问题的正确方法是什么吗?

class B
{
public:
B(int x1, int y1, int x2, int y2);
~B();

updateCoordinates(int x1, int y1, int x2, int y2);

private:
int x1;
int y1;
int x2;
int y2;
};

class A
{
public:
A(int mode);
~A();

private:
vector<B> bList;
};

A::A()
{
// Based on the value of mod, create
// objects of B and add to bList
}

最佳答案

class B
{
friend class A;
private:
B(int x1, int y1, int x2, int y2);
...
}

关于c++ - 嵌套类的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49451110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com