我们何时需要C++中的私有构造函数

39 c++ constructor private

我对c ++中的私有构造函数有疑问,如果构造函数是私有的,如何创建类的实例?我们应该在类中有一个getInstance()方法吗?

iam*_*ind 45

有一些private构造函数的场景:

  1. 除了friends之外,限制所有对象的创建; 在这种情况下,所有构造函数都必须是private

    class A
    {
    private:
       A () {}
    public:
       // other accessible methods
       friend class B;
    };
    
    class B
    {
    public:
       A* Create_A () { return new A; }  // creation rights only with `B`
    };
    
    Run Code Online (Sandbox Code Playgroud)
  2. 限制某些类型的构造函数(即复制构造函数,默认构造函数).例如std::fstream,不允许通过这种不可访问的构造函数进行复制

  3. 要有一个公共的委托构造函数,它不应该暴露给外部世界:

    class A
    {
    private: 
      int x_;
      A (const int x) : x_(x) {} // common delegate; but within limits of `A`
    public:
      A (const B& b) : A(b.x_) {}
      A (const C& c) : A(c.foo()) {}
    };
    
    Run Code Online (Sandbox Code Playgroud)
  4. 对于单例模式,当单例class不可继承时(如果它是可继承的,则使用protected构造函数)

    class A
    {
    public:
       A();
       A(int);
    private:
       A(const A&);  // Neither others nor `friend` can use this
       // A(const A&) = delete;  // C++11 equivalent `private` doesn't matter
    };
    
    Run Code Online (Sandbox Code Playgroud)

澄清:对于单例,一种典型的方法是public: static getInstance()在类中有一个方法,可以访问private构造函数.最终它创造并向外界提供对象.

    class Singleton
    {
    public:
       static Singleton& getInstance() {
          Singleton object; // invokes the `private` constructor
          return object;
       }
    private:
       Singleton() {}  // make `protected` for further inheritance
       Singleton(const Singleton&);  // inaccessible
       Singleton& operator=(const Singleton&);  // inaccessible
    };
Run Code Online (Sandbox Code Playgroud)

  • 实际上,如果该类是子类并且仅从子类使用,它可能是一个"受保护的静态". (6认同)

Mat*_* M. 12

私有构造函数通常与Builder方法一起使用,例如在Named Constructor惯用法中.

class Point
{
public:
  static Point Polar(double, double);
  static Point Cartesian(double, double);
private:
  Point(double,double);
};
Run Code Online (Sandbox Code Playgroud)

在这个(典型的)示例中,Named Constructor惯用法用于明确地使用哪个坐标系来构建Point对象.

  • 这是我所知道的最常见的情况.*builder方法*的另一个术语是*静态工厂方法*. (2认同)

Tun*_*her 11

当您想要控制类的对象创建时,Private Constructor非常有用.让我们尝试代码

#include <iostream>
using namespace std;
class aTestClass
{
    aTestClass()//////////private constructor of this class
    {
        cout<<"Object created\n";
    }
    public:

};
int main()
{
    aTestClass a;
    aTestClass *anObject;
}
Run Code Online (Sandbox Code Playgroud)

aTestClassa导致错误,因为这行间接尝试访问此行的私有constructor.com并运行程序.它运行绝对正常.现在问题是如何在这种情况下创建对象.让我们写另一个程序.

#include <iostream>
using namespace std;
class aTestClass
{
    aTestClass()//////////private constructor of this class
    {
        cout<<"Object created\n";
    }
    public:

    aTestClass* getAnObject()/////a public method create an object of this class and return the address of an object of that class
    {

        return (new aTestClass);

    }
};
int main()
{
    //aTestClass a;
    aTestClass *anObject=NULL;
    anObject=anObject->getAnObject();
}
Run Code Online (Sandbox Code Playgroud)

输出是

Object created
Run Code Online (Sandbox Code Playgroud)

所以我们创建了一个包含私有构造函数的类的对象. Use this concept to implement singleton class 谢谢