pointers and such
Okay, say I have a class like this:
class container
{
int *this_will_become_an_array; //using new int[40]
};
class reference
{
int *array;
};
Then i initialise the container array using new. What I try to do now is assign the reference''s "array" pointer to the array called this_will_become_an_array, so that they both point to the same data.
if have tried variations on this:
reference.array = container.this_will_become_an_array;
including &''s and ()''s and ->''s but to no avail.
Does anyone know how to do this? I would be grateful of some help as I have been tearing my hair out trying to solve this.
Thanks
wise_guy
quote:
Wiseguy
if have tried variations on this:
reference.array = container.this_will_become_an_array;
- You do realize that you can only manipulate ''data'' and not ''definitions'', do you?. Do you even instantiate those classes?
- Also, are you aware that these pointers are private datamembers?
If you said no on both questions, its time to get a book and get wise for real
This would work:
class BigArray {private: int *array;public: int *getArray(){ return array; } BigArray(){ array = new int[24]; } ~BigArray(){ delete [] array; }};class ReferenceArray {private: int * arrayReference;public: ReferenceArray(int *originalArray){ arrayReference = originalArray; } ~ReferenceArray(){ // DON''T DELETE! The BigArray class does that! }};void main(){ BigArray original; ReferenceArray reference( original.getArray() );}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement