Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将构造函数声明为私有将显示错误。是否至少必须有一个公共构造函数? 我是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; } 教材 { 私人: int*页; 整数*价格; Book()//默认构造函数 { pages=新int; 价格=新整数; *页数=300页; *价格=8; } 公众: void pre_析构函数() { std::cout_C++_C++11_Constructor_Destructor_Dynamic Memory Allocation - Fatal编程技术网

将构造函数声明为私有将显示错误。是否至少必须有一个公共构造函数? 我是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; } 教材 { 私人: int*页; 整数*价格; Book()//默认构造函数 { pages=新int; 价格=新整数; *页数=300页; *价格=8; } 公众: void pre_析构函数() { std::cout

将构造函数声明为私有将显示错误。是否至少必须有一个公共构造函数? 我是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; } 教材 { 私人: int*页; 整数*价格; Book()//默认构造函数 { pages=新int; 价格=新整数; *页数=300页; *价格=8; } 公众: void pre_析构函数() { std::cout,c++,c++11,constructor,destructor,dynamic-memory-allocation,C++,C++11,Constructor,Destructor,Dynamic Memory Allocation,否,公共构造函数不是强制性的。私有构造函数有一些用例 只有静态方法的类可能有私有的(或已删除的)构造函数,以防止创建实例 单例类(其中只存在一个类实例)可以通过使用private构造函数来强制其单例状态。该实例可以通过staticgetter访问 LI>您可能需要遵循一个构建器或工厂模式,在这里强制用户使用调用调用构造函数的过程来构造实例。该生成器或工厂将是类的成员或代码代码>,因此能够调用私有< /COD>构造函数。这种方案在爪哇比C++更常见。TC++也允许它。 也就是说,您只需要构造

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

  • 只有静态方法的类可能有
    私有的
    (或已删除的)构造函数,以防止创建实例
  • 单例类(其中只存在一个类实例)可以通过使用
    private
    构造函数来强制其单例状态。该实例可以通过
    static
    getter访问
  • <> LI>您可能需要遵循一个构建器或工厂模式,在这里强制用户使用调用调用构造函数的过程来构造实例。该生成器或工厂将是类的成员或代码<朋友>代码>,因此能够调用<代码>私有< /COD>构造函数。这种方案在爪哇比C++更常见。TC++也允许它。
也就是说,您只需要构造一个类的实例:

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;

}
#包括
课堂用书
{
私人:
int*页;
整数*价格;
Book()//默认构造函数
{
pages=newint();
价格=新整数();
*页数=300页;
*价格=8;
}
公众:
静态书本*constructBook()
{
归还新书();
}
void pre_析构函数()
{

std::cout一点也不!您不需要一个
公共构造函数,但您需要一个工厂函数来创建该类的实例&在这种情况下,您需要使用关键字
static
来限定该函数。请参阅以下内容:-

#include <iostream>
using namespace std;
class X
{
    int x;
    X(int x)
    { 
        cout<<"creation\n";
        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<<"destruction\n";
    }
};
int main()
{
    auto obj = X::make_X(5);
    auto ano = X::make_X(7);
    cout << obj.getx() << '\t' << ano.getx() << '\n';
    return 0;
} 
这里不需要公共控件。但是您需要
make\X
来创建类的实例

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

friend X make_X (int x);  // declare a prototype in the class
然后在类定义之外定义:-

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

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

将构造函数声明为private不允许在其他作用域中创建对象。因为不能从那里调用构造函数。因此构造函数/析构函数是公开的!您需要获取。
main没有在代码中直接访问构造函数。
也是。行
Book book1;
initial使用默认构造函数(将其设置为私有,因此无法访问)初始化对象请停止使用NeXT和Dele.这里需要动态内存分配吗?main应该返回0。为什么要访问析构函数的数据成员?@ PRASSIMEET:不是在析构函数中访问数据成员时有什么错误……停止使用“包含”。它不是标准C++。它只在GCC编译器中工作,并使程序生效。“这是不可移植的。”Pravasimet对此表示感谢,因此我做出了更改。这应该被接受为一个答案。回答不错。+1
X make_X (int x)
{
    return X(x);
}