如何从 C++ 中的主函数访问类中的私有构造函数?
How do I access a private constructor in a class from the main function in C++?
我有一个程序,我有一个名为Flower的超级类和子类Rose,Lily,Jasmine。
花保护成员,有2 个变量描述花的特征,公共成员有 getter 函数和一个纯虚函数(称为字符串 getColor())打印子类花的颜色。它还具有一个公共成员函数,用于打印每朵花的特征(void printFlower(字符串名称))。
我正在尝试为 Rose、Lily 和 Jasmine 实现该类,并且必须在不使用类中的公共访问修饰符的情况下执行此操作。因此,类中的所有内容都必须受到保护或私有(甚至是构造函数)。我知道Rose,Lily和Jasmine公开继承自父类Flower。
在我的主要功能中,我有这个:
int main(){
Rose rose;
Flower * pointer = &rose;
rose.printFlower("red");
Lily lily;
pointer = &lily;
lily.printFlower("white"); // not sure what color lilies are
Jasmine jas;
pointer = &jas;
jas.printFlower("orange");
return 0;
}
我的任务是不在 Rose、Lily 和 Jasmine 类中使用公共访问修饰符,但如果需要,我可以公开继承 Flower。那么,有没有办法使用私有构造函数(使用 Flower 作为朋友)或类似的东西来完成此操作?
编辑:我意识到我提供的信息很少,所以我在这里编辑问题。以下是我无法更改的花超类的声明。
class Flower{
protected:
bool isPopular;
bool isThorny;
public:
Flower(bool isPopular, bool isThorny){
this->isPopular = isPopular;
this->isThorny = isThorny;
}
bool getIsPopular(){
return this->isPopular;
}
bool getIsThorny(){
return this->isThorny;
}
virtual string getColor() = 0;
void printFlower(string name){
cout << "A " << name << " has '" << this->getColor() << "' color and is "
<< (this->getIsThorny()?"":"not ") << "thorny, and is " << (this->getIsPopular()?"":"not ")
<< "very popular." << endl;
}
};
我的任务基本上是图像中的以下内容: 用于声明派生类的任务
该任务将只运行 main 并在从 main 调用时打印 printFlower 函数。我唯一的限制是我不能在 Rose、Lily 和 Jasmine 的类声明中使用公共访问修饰符,但我可以并且确实公开继承了 flower。即。类玫瑰:公共花(){} 有什么方法可以通过将其所有成员设为私有或受保护来编写玫瑰、莉莉和茉莉花?
使用像这样的工厂函数
class FlowerFactory
{
Flower* getFlower(string flowerName)
{
if(flowerName == "Rose")
{
return new Rose();
}
if(flowerName == "Lily")
{
return new Lily();
}
if(flowerName == "Jasmine")
{
return new Jasmine();
}
}
}
//Base class
class Flower
{
};
class Jasmine : public Flower
{
//other things here..
friend FlowerFactory;
};
class Rose : public Flower
{
//other things here..
friend FlowerFactory;
};
class Lily : public Flower
{
//other things here..
friend FlowerFactory;
};
你显然被引导到对工厂模式的某种看法。解决方案是在Flower
中定义一个工厂函数,该函数将执行实例化。
我将展示如何做到这一点。为了说明为什么这可能有用,我将把工厂变成一个简单的转换器,从运行时参数到编译时类型:
class Flower
{
public:
virtual ~Flower() = default;
static std::unique_ptr<Flower> create(const std::string &species)
{
std::unique_ptr<Flower> result;
if (species == "rose")
{
result = std::make_unique<Rose>();
}
else if (species == "lily")
{
result = std::make_unique<Lily>();
}
else
{
throw std::invalid_argument("No such flower");
}
return result;
}
};
class Rose : public Flower
{
friend Flower;
Rose() = default;
};
class Lily : public Flower
{
friend Flower;
Lily() = default;
};
int main(int argc, char **argv)
{
auto rose = Flower::create("rose");
auto lily = Flower::create("lily");
auto whatTheUserRequested = Flower::create(argv[1]);
}
您的指示是不使用访问说明符,还是您的成员都不能是私有的?如果是前者,则默认情况下可以使用struct
而不是class
来获取公共访问权限。例如:
struct Rose : Flower {
Rose() : Flower(...) {}
string getColor() const override { return "red"; }
};
- 固有构造函数的字符和访问级别
- C++:为什么无法在派生类中访问受保护的构造函数?
- 使用 CTRP 时,是否访问访问父构造函数 UB 中的子属性?
- c++ cuda:cudaMalloc在构造函数之外的托管访问
- 使用值初始化访问构造函数?
- 限制variadic模板类中的构造函数访问
- 构造函数无法访问继承类私有 int
- 从派生类访问模板基类的构造函数
- 如何在构造函数中访问类变量以分配它们,而无需在C++中使用此指针
- 访问在构造函数期间创建的值 - C++
- 从析构函数访问模板类构造函数的参数,可以吗?
- 仅当构造函数具有参数时,C++ 公共成员才能访问
- 正确的友元定义,以授予 std::map 对私有默认构造函数的访问权限
- 在派生类的构造函数初始化中无法访问受保护的函数
- 是否可以从 C++ 中的构造函数访问对象名称?
- 从构造函数访问值
- 聚合初始化不支持构造函数访问
- 无法从构造函数访问私有变量 - 不在范围(C )中
- c++工厂.子构造函数不能从父构造函数访问
- 如何通过类构造函数访问私有静态成员变量