Two pointers point to the same refernce [closed]
I have a question about the pointers
I have two pointers, one is initialized and the other is not;
Now i want the second pointer that has no value (is not initialized yet) to point to the same place in the memory.
OK, i wrote a small program to do this and it works correctly
int *P , *P2 ;
P = new int ;
P2 = new int ;
*P = 1 ;
P2 = P ;
cout << "P= " << *P << endl << endl ;
cout << "P2= " << *P2 << endl << endl ;
*P = 0 ;
cout << "P2= " << *P2 << endl << endl ;
The output is like this:
P = 1 ;
P2 = 1 ;
P2 = 0 ;
So it work correct like i want.
Now i want to do the same but this time i want to do it using ID3D11Device *
Here is the code:
ID3D11Device *Test ;
Test = Device->Get_Device() ;
cout << "Test =" << Test << endl << endl ;
cout << "Get = " << Device->Get_Device()<< endl << endl ;
Device->~CL_Device();
cout << "Test =" << Test << endl << endl ;
cout << "Get = " << Device->Get_Devic开发者_开发问答e()<< endl << endl ;
Get_Device function definition :
![ID3D11Device *const Get_Device() const { return _Device ;}][1]
schema explaining what i want.
First, you should avoid calling the destructor of an object directly. That does not free the memory associated with it. Use delete Device;
instead.
Secondly, if you want two pointers you simply have to proceed as you hav shown in your first example:
ID3D11Device *Test, *Test2;
Test = Device->Get_Device();
Test2 = Test;
now Test
, Test2
and Device->Get_Device()
all point to the same location in the memory of course only if Device->Get_Device()
returns always the same pointer.
EDIT: see comment
精彩评论