将构造函数声明为private会显示错误.是否至少有一个公共构造函数是必需的

Declaring constructors as private shows errors. Is at least one public constructor mandatory?

本文关键字:构造函数 有一个 是否 private 声明 显示 错误      更新时间:2023-10-16

我是C++的学习者。我编制了以下程序。我正在研究构造函数和析构函数的概念。我在下面的代码中声明了一个私有析构函数,并使用main()类的成员函数访问私有成员。我知道可以声明私有构造函数,但公共构造函数也是强制性的吗?下面是我的代码:

class Book
{
private:
    int *pages;
    int *price;

    Book()        //default constructor
    {
        pages = new int;
        price = new int;
        *pages = 300;
        *price = 8;
    }
public:
    void pre_destructor()
    {
        std::cout << "The pages:" << *pages << "n";
        std::cout << "The price:" << *price << "n";
    }
~Book() //destructor
    {
        std::cout << "The pages:" << *pages << "n";
        std::cout << "The price:" << *price << "n";
        delete pages;
        delete price;
    }
};
int main()
{
    using namespace std;
    Book book1;
    cout << "Before using destructors" << endl;
    cout << "---------------------------------"<< endl;
    book1.pre_destructor();
    cout << "After using destructors" << endl;
    cout << "---------------------------------";
    return 1;
}

对于上面的程序,显示了两个错误。一个是在声明对象的主函数中;错误:内容中有错误。第二个是在调用构造函数的行中;错误:Book::Book()是私有的。

main没有直接访问代码中的构造函数。那么为什么会显示访问错误呢?

否,public构造函数不是强制性的。私有构造函数有一些用例。

  • 只有静态方法的类可能具有private(或已删除)构造函数,以防止创建实例
  • 单例类(其中只存在该类的一个实例)可以通过具有private构造函数来强制其单例状态。该实例可以通过static getter访问
  • 您可能希望遵循构建器或工厂模式,强制用户在调用构造函数的同时使用流程来构建实例。该构建器或工厂将是类的成员或friend,因此能够调用private构造函数。这种方案在Java中比C++更常见,但C++也允许它

也就是说,您希望简单地构建一个类的实例:

Book book1;

这种使用肯定需要public默认构造函数。

当您将构造函数设为私有时,您需要公开一个方法,以便外部类/方法可以创建此类的对象。您可以通过创建一个静态方法来实现这一点,该方法反过来又会创建对象。

以下代码演示:

#include <iostream>
class Book
{
private:
    int *pages;
    int *price;

    Book()        //default constructor
    {
        pages = new int();
        price = new int();
        *pages = 300;
        *price = 8;
    }
public:
    static Book* constructBook() 
    {
        return new Book();
    }
    void pre_destructor()
    {
        std::cout << "The pages:" << *pages << "n";
        std::cout << "The price:" << *price << "n";
    }
    ~Book() //destructor
    {
        delete pages;
        delete price;
    }
};
int main()
{
    Book* book1 = Book::constructBook();
    std::cout << "Before using destructors" << endl;
    std::cout << "---------------------------------"<< endl;
    book1->pre_destructor();
    cout << "After using destructors" << endl;
    cout << "---------------------------------";
    delete book1;
    return 0;
}

一点也不!您不需要public constructor,但需要一个工厂函数来创建该类的实例&在这种情况下,您需要使用关键字static来限定该函数。见下文:-

#include <iostream>
using namespace std;
class X
{
    int x;
    X(int x)
    { 
        cout<<"creationn";
        this->x = x; 
    }
    public:
    static X make_X (int x) // without static error message will be : error: cannot call member function 'X X::make_X(int)' without object
    {
        return X(x);
    }
    int& getx()
    {
        return x;
    }
    ~X()
    {
        cout<<"destructionn";
    }
};
int main()
{
    auto obj = X::make_X(5);
    auto ano = X::make_X(7);
    cout << obj.getx() << 't' << ano.getx() << 'n';
    return 0;
} 

输出为:-

creation
creation
5   7
destruction
destruction

这里不需要公共承包商。但是您需要make_X来创建该类的实例。

您也可以使用friend而不是静态。在这种情况下,代码将是:-

friend X make_X (int x);  // declare a prototype in the class

然后在类定义之外定义:-

X make_X (int x)
{
    return X(x);
}

这就是它的工作原理!干杯