Q: Void print (int arr[][], int size); Is this function declaration right or wrong? Of course it is wrong, this is not a function of the declaration of the problem, but the problem of the array declaration, int arr[][] This statement is wrong, the length of the latter dimension can not be omitted!
Q: char* screeninit (int height = +, int width, char background); How about this statement?
Wrong, if you specify a default value for the parameter height, the subsequent parameters must both specify a default value.
functions defined inside the class default to the inline class
How to use C + + pointers
Why use both the * and & symbols in the following function declarations? And on what occasions to use this declarative approach?
void Func1 (MYCLASS *&pbuildingelement);
Such questions are often asked in the forum. This article attempts to explain this problem by using some practical pointers to experience.
It's a bit confusing to take a closer look at this way of declaring. In a sense, "*" and "&" are two things that are relative to each other, what is the point of putting them together? In order to understand this approach to pointers, let's review the ubiquitous pointer concept in C + + programming. We all know what myclass* means: A pointer to an object that is of type MyClass. Void func1 (Myclass*pmyclass);
For example: myclass* p = new MYCLASS;
Func1 (P);
This method of handling the above code must have been used, create a MyClass object, and then pass it to the FUNC1 function. Now suppose this function wants to modify PMYCLASS:VOIDFUNC1 (Myclass*pmyclass)
{
DoSomething (Pmyclass);
Pmyclass =//pointers to other objects
}
The second statement modifies only the value of Pmyclass during the function. The value of the caller's variable p is not modified. If P points to an object at address 0x008a00, it still points to this particular object when Func1 returns. (This is entirely possible unless the func1 bug will mess up the heap.) )
Now suppose you want to modify the value of P in func1. This is your right. The caller passes in a pointer, and the function assigns a value to the pointer. It used to be a double pointer, a pointer to a pointer, for example, cmyclass**.
myclass* p = NULL;
Func1 (&P);
VOIDFUNC1 (Myclass**pmyclass);
{
*pmyclass = new MYCLASS;
......
}
After calling Func1, p points to the new object. In COM programming, you will encounter this usage everywhere-for example, in the QueryInterface function of the query object interface:
Interface ISomeInterface {
HRESULT QueryInterface (Iid&iid, void** ppvobj);
......
};
Lpsomeinterface P=null;
Pob->queryinterface (IID_SOMEINTERFACE,&P);
Here, p is a pointer to the Someinterface type, so &p is a pointer to the pointer, and if the call succeeds when the QueryInterface returns, the variable p contains a pointer to the new interface.
If you understand pointer pointers, then you definitely understand pointer references, because they are completely the same thing. If you declare the function as follows:
void Func1 (Myclass*&pmyclass);
{
Pmyclass = new MYCLASS;
......
}
In fact, it is matter with pointers to pointers in the previous example, except that the syntax is different. Pass the time not to preach P address &p, but directly to the P itself:
myclass* p = NULL;
Func1 (P);
After the call, p points to a new object. Generally speaking, the principle of reference is more or less like a pointer, syntactically it is a common variable. So as long as you meet *&, you should think of * *. This means that the function modifies or possibly modifies the caller's pointer, and the caller passes the pointer like a normal variable without using the address operator &.
As for what occasion to use this method, I would say, very little. MFC uses it in its collection class--for example, CObList, which is a list of cobjects pointers.
Classcoblist:public Cobject {
......
Gets/modifies the element at the specified position
cobject*& GetAt (positionposition);
cobject* GetAt (positionposition) const;
};
There are two getat functions, all of which are the elements that get the given position. What's the difference?
The difference is that one lets you modify the object in the list, and the other does not. So if you write the following: Cobject*pobj = MyList. GetAt (POS);
POBJ is a pointer to an object in the list, and if you then change the value of POBJ: POBJ = psomeotherobj;
This does not change the object address at POS, but only changes the variable pobj. But if you write the following: cobject*& rpobj= mylist. GetAt (POS);
Rpobj is now a pointer to an object in a list, so changing the rpobj will also change the address of the object at POS in the list-in other words, instead of the object. That's why CObList has two getat functions. One can modify the value of the pointer, and the other cannot. Notice what I'm saying here is the pointer, not the object itself. Both functions can modify the object, but only the *& version can replace the object.
It is important to refer to C/s + + and it is also an efficient means of processing. Therefore, in order to become a C + + master, the concept of reference is not a thorough understanding and skilled application is not possible.