confused with pointer to the class structure

Started by
3 comments, last by chairthrower 15 years, 9 months ago
I am confused with the pointer to the class structure, why i have to declare a double pointer to the class just to actually hold the whole data in the class? i mean, for e.g: class Human { public : Human * myFriends; // dynamic memory } void main (){ Human Daniel; Human * ptrHuman1; Human ** ptrHuman2; ptrHuman1 = Daniel; // have problems here ptrHuman2 = & Daniel; } I am a bit unclear about the concept in pointer to class structure. what does single pointer, ptrHuman1 points to since it's not pointing to the address of Human class? And why should i have to declare a double pointer just to hold the dynamic memory in Human class? Thanks in advance.
Technical ..? then Master Non-Technical first ...
Advertisement
You don't have to declare a double pointer:
class Human{	public:		Human * myFriends; // dynamic memory}// void main() is not and never has been a valid C++ entry pointint main (){	Human Daniel;	Human * ptrHuman1 = &Daniel;}
Σnigma
Quote:And why should i have to declare a double pointer just to hold the dynamic memory in Human class?


Intuiting from the description - you want the myfriends to contain zero or more other humans. therefore this member cannot merely be a pointer to a single Human object. Instead it must be or point at a data structure that contains pointers to other humans. a contiguous array can satisfy the need and this will lead to the pointer to pointer semantics using c style declarations.

struct Human{    Human   **myfriends;    // plural   };int main(){    Human   *daniel = new Human;     Human   *ezekiel = new Human;     Human   *azariah = new Human;      daniel->myfriends = new Human*[ 2];     daniel->myfriends[ 0] = ezekiel;        daniel->myfriends[ 1] = azariah;    };


note that in c++ its more natural to use a container type to manage collections like this - this helps because all the pointer to pointer semantics gets kind of clumsy and is complicated to remember and get right. in this case myfriends could be declared as std::vector< Human *> myfriends;

As i say i am guessing a bit at the problem in a global fashion - so hopefully i didnt miss anything.
thanks for help,
maybe myFriends got a bit confusing to this,
but assume that i am only adding ONE friend to this.
so ,
my problem here i try to describe is,

when i try something like :


Human Daniel;
Human Peter;

Human * People = new Human [2];

Daniel->myFriends = Peter;
Peter->myFriends = Daniel;

People[0] = Daniel;
People[1] = Peter;

People[0].myFriends.ChangeName("Simon"); // assume change name is to change a name.

cout << Peter->myName << endl; // this shows 'Peter'
cout << People[0].myFriends->myName << endl; // this shows 'Simon'


i don't know why it is not pointing to the same class "Peter" if i use "People" as a single pointer.


But when i try
Human ** People = new Human * [2];
People[0] = &Daniel
People[1] = &Peter

it works fine and perfect.

why is it?
Technical ..? then Master Non-Technical first ...
Human Daniel;Human Daniel;Human Peter;Human * People = new Human [2];People[0] = Daniel;People[1] = Peter;


these last two statements are not doing what you think they're doing or what you want them to do. here, when you assign (using the '=' operator) you are invoking the Human assignment operator and not the implicit pointer assignment operator. If you have seen this before in c++ it should ring a bell, otherwise know that you are copying the entire object and not copying the pointer to the object.

It might help when trying to figure this out to output the addresses of the objects

Human * People = new Human [2];People[0] = Daniel;cout << &Daniel << endl;cout << &people[ 0] << endl;


this should show two different addresses for the two different objects. Because you have object copies rather than copies of the referereces to the objects the data they contain will obviously be different.

From the posted code, something similar is going on (copying the object rather than the pointer) with the myfriends. the new declaration of myfriends is not posted but if myFriends is used in the context of People[0].myFriends.ChangeName("Simon") then it must be declared as Human myfriends rather than Human *myFriends or Human **myFriends which will also imply an object instance copy.

My suggestion in experimenting with this stuff would be to avoid declarations like this
Human Daniel;

and instead opt for something like as the intial Human creation
Human *Daniel = new Human


This at least creates a Human object that is dynamically allocated and then can be referenced with these types of assignments.
Humans **humans = new Human*[2];humans[ 0] = Daniel;              // pointer assignment or Human  *single_friend = Daniel;    // another pointer assignment


this is for two reasons - it avoids copying objects unnecessarily and also its more consistent with dynamically allocated objects generally since a declaration like
Human Daniel; Human *Daniel_ptr = &Daniel; 

is not dynamic allocation (the object Daniel will be destroyed at the end of the function (main() ).

This topic is closed to new replies.

Advertisement