Transferred from: http://www.blogjava.net/fhtdy2004/archive/2009/05/30/278971.html
In many cases, only one object is required in the current program. For example, a program has only one connection to the database and only one mouse object. Usually we put the declaration of the constructor in the public section, what happens if we put it in the private section? What does that mean?
(1) constructor definition private
When we declare an object in a program, the compiler calls the constructor (if any), and the call is usually external, that is, it is not a call to the class object itself, and if the constructor is private, it will result in a compilation error because it is not allowed to access private members outside of class.
However, for the class itself, it can take advantage of its static public members, because they are independent of the class object and can be used without having to produce objects.
At this point, because the constructor is privatized by class, we have to be able to access the private domain of class by creating the object, but only the members of class can do it, but how can we make use of its members before we construct its object? Static public member, which is independent of the class object, and "we" can be accessed. If you create an object of that class in a static function and return it as a reference or as a pointer (this is not returned as an object, the constructor is private, and the external cannot create a temporary object), the object is granted the right to use.
Here is an example:
Class Onlyheapclass
{
Public
Static onlyheapclass* getinstance ()
{
Creates a Onlyheapclass object and returns its pointer
Return (new Onlyheapclass);
}
void Destroy ();
Private
Onlyheapclass () {}
~onlyheapclass () {}
};
int main ()
{
Onlyheapclass *p = Onlyheapclass::getinstance ();
...//using *p
Delete p;
return 0;
}
This example uses a private constructor, getinstance () as a static member function of Onlyheapclass to create an object in memory: because it is passed across functions and cannot use value passing, we choose to create objects on the heap, so that even if getinstance () Exits, the object is not released and can be released manually.
The design of a class that is privatized by a constructor guarantees that other classes cannot derive from or create instances of the class, and that it is useful for example to implement a class that has at most one in memory or a specified number of objects (you can add a static type counter to a private domain in class) , its initial value is set to 0, and then in getinstance () to make some restrictions: each time it is called to check whether the value of the counter has reached the upper limit of the number of objects, if it is an error, otherwise new objects, Increase the value of the counter by 1 at the same time. Finally, to avoid creating a new copy of the object when the value is copied, the copy constructor is specifically declared and private, in addition to the constructor being private.
If you design a constructor as protected, you can do the same for the same purpose, but you can inherit it.
(2) destructor private
How else do you guarantee that you can only new a class object on the heap? Simply define the destructor as a private member.
The reason is that C + + is a statically bound language. All non-virtual function calls must be parsed as part of the compilation process. Even virtual functions need to be checked for accessibility. As a result, when an object is generated on the stack, the object is automatically refactored, and the destructor must be accessible. The build object on the heap, because the time of the destruction is controlled by the programmer, so it is not necessary to destructor. After you have guaranteed that you cannot generate an object on the stack, you need to prove that it can be generated on the heap. The only difference between onlyheapclass and a generic object here is that its destructor is private. The delete operation invokes the destructor. So it cannot be compiled.
So how do you release it? The answer is also simple, providing a member function to complete the delete operation. In member functions, destructors are accessible. Of course Detele operations can also be compiled through.
void Onlyheapclass::D Estroy () {
Delete this;
}
The design of the class for the privatization of the constructor guarantees that the object can only be generated in the heap with the new command, and that the object can be created dynamically, thus allowing the object's life cycle to be freely controlled. However, such a class would need to provide a common interface for creation and revocation.
In addition the overloaded delete,new for private can achieve the purpose of requiring the object to be created on the stack, with placement new can also be created on the stack.
--------------------------------------------------------------------------------------------------------------- ------------------
still do not understand AH:
1. Why do you call it yourself? Do you want the destructor to be called automatically when the object ends its lifetime? Under what circumstances do you need to call destructors yourself?
In a situation like this, you want to do something before you refactor, but you don't know what your class is.
Then you can re-write a function that takes all the things you want to do before you call the destructor.
So that they can only call you the function of the destructor, so that the destruction must be done before the action you require.
2. Under what circumstances is it useful to have only a pile of objects?
The heap object is new, relative to the Stack object. Under what circumstances should new, under what circumstances be inside the stack
Early allocation, is nothing more than when to use the dynamic, when the problem of static generation. This should be based on the specific circumstances
Specific analysis. For example, if you know in advance that an object can be up to 10 in a function, you can
Defines an array of this object. 10 elements, each of which is a stack object. If you can't determine the number
, then you can define a pointer to this object, create a new one, and use the list
or vector management.
--------------------------------------------------------------------------------------------------------------- ------------------
The meaning of the "private" permission in a class is that a private member can only be accessed within the class domain and cannot be accessed outside the class.
By defining the destructor as private, the user is prevented from using the destructor in the domain of the class. This is reflected in the following two areas:
1. Prohibit users from defining variables of this type, that is, to disallow the creation of objects of this type within the stack memory space. To create an object, you can only use new on the heap.
2. Prohibit users from using delete in the program to delete this type of object. Object deletion can only be implemented within the class, that is, only the implementation of the class can implement the delete of the object, the user can not easily delete the object. If the user wants to delete the object, it can only be done according to the method provided by the class's implementation.
Visible, this greatly limits the user's use of this class. Generally do not do this; this is usually done for special purposes, such as in the implementation of Singleton. Landlord can find singleton information to understand how it is one thing.
A constructor or destructor in C + + is defined as private