将构造函数声明为private会显示错误.是否至少有一个公共构造函数是必需的
Declaring constructors as private shows errors. Is at least one public constructor mandatory?
我是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);
}
这就是它的工作原理!干杯
相关文章:
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 关于复制构造函数的一个棘手问题
- C++构造函数,一个用于度,一个用于弧度
- 在成员初始值设定项列表和构造函数主体中委派构造函数有什么区别
- 关于QLIST类型实例复制构造函数的一个奇怪问题
- 删除默认类构造函数有什么意义?
- 如何应用使默认构造函数有条件地显式
- 两种类型的定义构造函数有什么区别?第一个使用":",第二个用大括号(如函数)定义它
- SquareMatrix 的 ADT 构造函数有什么问题?
- 需要使班级(long_number)用构造函数和一个打印功能代表1到60的任何长度
- 运算符 = 不匹配,构造函数创建一个指针
- 双重列表复制构造函数:与单一列表复制构造函数有何不同
- 为什么我不能在 c++ 类中声明一个空构造函数,该构造函数从一个具有私有构造函数的构造函数扩展而来
- 如何使构造函数接受一个非常量对象
- 初始值设定项列表,可变参数模板和构造函数推导:一个奇怪的情况
- shared_ptr的原始指针构造函数是一个错误吗?
- 通过初始化列表调用另一个类的构造函数.有问题
- 一个非平凡的复制构造函数有什么作用
- 有一个转换构造函数有什么意义?
- 按值传递给函数和按值传递给另一个构造函数时,调用构造函数有什么不同?