How to use constructors

1: Define a class that cannot be inherited

We can declare an auxiliary class A, declare the constructor of class A as private, and make B a friend class of a, and B virtual inherits a. In this way, other classes cannot inherit B.

Suppose that there is a class D inheriting B, and the constructor of class B will be called first when D initializes, but because B inherits a virtually, in order to prevent ambiguity, d must first call the constructor of A. because the constructor of a is private and inaccessible, errors will be generated and inheritance will fail.

#include<iostream>
#include<iomanip>
using namespace std;
 
 
//Design a class that cannot be inherited
 
class B;//Forward declaration
class A{
	friend B;
private:
	A(){}
};
class B : virtual public A{
public:
	B(){cout << "Hello" << endl;}
};
//Class D: public B {/ / error c2248: "A::A": unable to access [private member]
//The constructor of public://A is not accessible, resulting in an error
//	D(){}
//};
 
void main()
{
	B b;
	//D d;
}

2: A class that can only generate objects on the stack

Objects can be generated on the stack or we can generate new objects on the heap. To generate objects only on the stack, but not on the heap outside the class

It can't be done. We have to start from within the class.

We know that the construction of class objects is done by constructors. If we declare the constructors as protected, we cannot call them outside the class

Constructor. In this way, the object cannot be new outside the class, but at the same time, the object cannot be defined outside the class.

To do this, we can generate an object within the class and provide a public interface to return the object. Next, if you define a class outside the class

Object, just call this function. It seems that this does not work, because class member functions can only be called by class objects outside the class, which

What should I do??? We can declare this interface as a static member function, so we can.

#include<iostream>
#include<iomanip>
using namespace std;
 
 
class CObjOnlyInStack{
public:
	static CObjOnlyInStack GetObjInst(int data){return CObjOnlyInStack(data);}
	void DispSelf(){cout << "data : " << iData << endl;}
protected:
	CObjOnlyInStack(int data){iData = data;}
private:
	int iData;
};
 
 
void main()
{
	//"Cobyonlyinstack:: cobyonlyinstack": unable to access [protected member]
	//CObjOnlyInStack oObjOnlyInStack(10);
	//CObjOnlyInStack* pObjOnlyInStack = new CObjOnlyInStack(100);
	
	CObjOnlyInStack oObjOnlyInStack = CObjOnlyInStack::GetObjInst(1000);
	oObjOnlyInStack.DispSelf();
}

3: A class that can only generate objects on the heap (the analysis is the same as above)

#include<iostream>
#include<iomanip>
using namespace std;
 
class CObjOnlyInHeap{
public:
	static CObjOnlyInHeap* GetObjInst(int data){return new CObjOnlyInHeap(data);}
	void DispSelf(){cout << "data : " << iData << endl;}
protected:
	CObjOnlyInHeap(int data){ iData = data;}
private:
	int iData;
};
 
void main()
{
	//"Cobyonlyinhead:: cobyonlyinhead": unable to access [protected member]
	//CObjOnlyInHeap* pObjOnlyInHeap = new CObjOnlyInHeap(10);
 
	CObjOnlyInHeap* pObjOnlyInHeap = CObjOnlyInHeap::GetObjInst(100);
	pObjOnlyInHeap->DispSelf();
}

 

Posted by Ali25m on Thu, 09 Jan 2020 11:16:42 -0800