How to make a class

Started by
15 comments, last by kingpinzs 20 years, 6 months ago
can anyone tell me why when you created a object on the heap you have to makeit a pointer...and when this happens you cant access any of class members or functions
Advertisement
No idea
quote:Original post by Rydis
can anyone tell me why when you created a object on the heap you have to makeit a pointer...and when this happens you cant access any of class members or functions


Well if you do something like this where Alien is a struct or a class with a default constructor:

Alien * greenMartian = new Alien;

All you've really done is created a pointer, not an Alien object itself. greenMartian, in its current state, points to a section of memory big enough to hold an Alien object. So doing this and assuming eyes is an int that your object can store:

cout << greenMartians->eyes;

That will get you something wierd until you actually initalize the object the being pointer at. I sometimes mix this up, but I don't think the constructor gets called when you do what I did above when I called new. If I am wrong on that point then the problem might be that your constructor doesn't do anything. Does your class's constructor assign default values to its data members? Otherwise its like doing this:

int whatever;
cout << whatever;

Someone feel free to correct me if I am wrong about when the constructor gets called.


[edited by - logic monkey on October 6, 2003 10:12:49 PM]
ya it has assingments in the default constructor....but from what i get that you said...a pointer that points to an object dont seem to be right in the case...case the compiler im using as school(vc++.net) regonizes it clearly as a pointer to an object with no data members or functions...would this be fixed by using -> instead of the .. cause using the dot operator is useless so something like

Auction *newAuction = new Auction();
newAuction.Item; //this is impossiable cause its saying newAuction dont have the data member Item...but if you did
Auction newAuction;
newAuction.Item; //this is possiable

so will using -> fix this or what?
quote:Original post by Rydis
So will using -> fix this or what?

Yes.

newAuction->Item is shorthand for (*newAuction).Item, which dereferences newAuction before it accesses the data field. You obviously have to do this if you''re using a pointer.
I had the exact same problem with my class.
error dont have the data member Item

[edited by - kingpinzs on October 6, 2003 11:36:37 PM]
quote:Original post by Rydis
ya it has assingments in the default constructor....but from what i get that you said...a pointer that points to an object dont seem to be right in the case...case the compiler im using as school(vc++.net) regonizes it clearly as a pointer to an object with no data members or functions...would this be fixed by using -> instead of the .. cause using the dot operator is useless so something like

Auction *newAuction = new Auction();
newAuction.Item; //this is impossiable cause its saying newAuction dont have the data member Item...but if you did
Auction newAuction;
newAuction.Item; //this is possiable

so will using -> fix this or what?


Yep, if the thing on the left of where you need to use the dot operator is a physically just a pointer, using the -> operator is the way to get to your data correctly. If it''s not a pointer, use the dot like usual. Sorry for answering a different question than the one you asked.

This topic is closed to new replies.

Advertisement