answersLogoWhite

0

Yes. However, like any other private member, a private constructor can only be accessed by the class itself (typically via a static member function) or by one of its friends.

There are very few cases where private constructors are appropriate, one of the most common being the need to suppress the compiler-generated copy construction of a base class. However, since C++11, suppressed constructors can simply be deleted, thus making error messages much more meaningful to users of your class.

For example, instead of the following:

class A {

public:

A (); // default constructor

private:

A (const A&); // suppress copy constructor (can still be invoked by the class and its friends)

// ...

};

You'd now use the following:

class A { public: A (); // default constructor

A (const A&) =delete; // suppress copy constructor (cannot be invoked at all)

// ...

};

User Avatar

Wiki User

8y ago

What else can I help you with?

Related Questions

Is Constructor Can be Private and Protective in c?

question i understand not


Can you write own constructor in c plus plus?

Yes.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


What if you declare public members rather than private in c plus plus?

Public members in C++ have accessibility to any function that has scope to the instance of the class, whereas private members have accessibility only to functions of that class.


Can you declare a method within a method in c or c plus plus?

C: there are no methods in C. C++: no.


Can you make private class in c plus plus?

yes it is possible to make a private class in C++ but this class will not solve any purpose.................


What are different type of constructor in java?

Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


Write a program in c plus plus with constructor?

// constructor program to add two number's // program written by SuNiL kUmAr #include<iostream.h> #include<conio.h> class constructor { private: int a,b; public: constructor(int m,int n); int sum(); }; constructor::constructor(int m,int n) { a=m; b=n; } int constructor::sum() { int s; s=a+b; return (s); } int main() { int x,y; clrscr(); cout<<"enter two number's to add \n"; cin>>x>>y; class constructor k (x,y); cout<<"sum of two number's is = "<<k.sum(); getch(); return (0); }


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.