At school they though us it is just a "storage room" for an adress. But if it was only that, why do we den have diffrent types of pointers. Like a pointer to an object from class A and an other one that points to an object from Class B. And pointer that points at A, can't be used to point to B....why is that? If it was just a adrees it would not care...would it?
Not sure why I have never thought about this before.....hmmmm...
ØØ
Last edited by NoteMe; Jul 11th, 2004 at 05:22 PM.
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
A pointer is a memory address (doesn't allocate storage its self). I think the reason why it is typed (only points to a given thing) is that it improves code readability, plus it allows compilers to do various optimisations. I'd guess it allows compilers to do their compiling faster as well.
When you do something like this:
cClassA *pA = new cClassA;
it's the 'new' keyword that allocates the memory storage space, then it returns the address of that storage space (which gets assigned to the pointer pA).
You can make a pointer that can point to anything by making it a void pointer:
void *pPointer
When you want to access the members of a class that it points to you have to type cast it, so the compiler knows what memory location within the class you are accessing (i think).
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Yeah I know that it is the new keyboard that allocates the new space. I was just wondering why you can't use a pointer to more the one type of object.
So if it is like you said, then the compiler must remember what kind of pointer it is. And there is nothing stored in the app about what kind of pointer it is.
But if you are using a inheritense then it is not that way. Can't remember what way it is, but if you have a base class Shape, then two child classes that derives from shape, called square and circle, then you can use a square pointer to point to a shape object or the other way around....can never remember what way it is. Just thought that if I found out what makes a pointer able to only point to one type of objects I would remember what way the pointers work when it comes to inheritense (SP?)
the reason you have typed pointers is for determining length. If I have a pointer to a 16-bit int, then the compiler knows that it needs to read 2bytes to get the value, while a 32-bit int needs 4. String pointers meanwhile point to jsut the begining (1 byte) and it's up to you to determine the end.
Ok, I'll not refer to existing posts, so this is going to be long.
In essence, a pointer is a variable that holds a memory address. void * is the simplest pointer, because it ONLY holds an address.
But most pointers are typed. The compiler knows that they refer to a specific type. This is for several reasons.
First, size. The pointer type tells the compiler how large the referenced object is, which is necessary for pointer arithmetics, like incrementing.
Second, type checking. C++ is a strongly typed language and enforces type compatibility. If a pointer had no type attached, this wouldn't be possible.
Third, availability of methods and properties. When you do ptr->do(), the compiler needs to know what do method you're referring to, more than one class could have one. Thus the pointer needs a type.
All of these are compile-time checks, the pointer stores no such information in the finished program.
Polymorphic classes are special cases. You can assign the address of an object to a pointer that is actually of base class type. This is possible because the derived class fulfills all requirements of the base class, thus the compiler cannot err when resolving method calls and property accesses as if it WAS a base class object.
Polymorphism itself is resolved differently, but it doesn't directly have anything to do with the pointer.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
OK...thanks for the good explenation. So just to wrap it up here.
If I hava a base class shape. And then have two derived clases Circle and Rectangle. Then if I make a pointer to a shape then I can use that pointer to point to a Circle or Rectangle too. And use ALL thir function. Even if they are not inherited from the base class? Or is it the other way around?
Hmmm...one more problem though. If I have a base class with a virtual function like this.
virtual void compute() = 0;
then all the objects on the screen are supposed to derive from this class. But how can I make a pointer to the base class point to a derived object?
If the base class is called cOnScreen, and one derived class is called cTree1 and one of the cTree1 objects are called tre1, then this does at least not work.
Originally posted by NoteMe Hmmm...one more problem though. If I have a base class with a virtual function like this.
virtual void compute() = 0;
then all the objects on the screen are supposed to derive from this class. But how can I make a pointer to the base class point to a derived object?
If the base class is called cOnScreen, and one derived class is called cTree1 and one of the cTree1 objects are called tre1, then this does at least not work.
cOnScreen* screen = &tre1;
virtual void compute() = 0;
That is a pure virtual function. It makes your base class an abstract class. If you don't override it in your derived classes, they also will become abstract.
Originally posted by NoteMe At school they though us it is just a "storage room" for an adress. But if it was only that, why do we den have diffrent types of pointers. Like a pointer to an object from class A and an other one that points to an object from Class B. And pointer that points at A, can't be used to point to B....why is that? If it was just a adrees it would not care...would it?
Not sure why I have never thought about this before.....hmmmm...
ØØ
Well internally that is all a pointer is, just a 32 bit address. The compiler forces rules on you however, with how you are able to manipulate that address.
The magic of polymorhism in C++ is done with whats called a v-table. That's how the compiler keeps track of the objects.
Education is an admirable thing, but it is well to remember from time to time that nothing that is worth knowing can be taught. - Oscar Wilde
Originally posted by Maven Well internally that is all a pointer is, just a 32 bit address. The compiler forces rules on you however, with how you are able to manipulate that address.
The magic of polymorhism in C++ is done with whats called a v-table. That's how the compiler keeps track of the objects.
Yeah I have figgured that now. But as long as I only inherits from one base class, the derived classes will only have one v-table. And I wanted to test the perfomance of it and see how much slower it is then not having a v-table.
What I want is actualy to have a pointer that can point to what ever object that has derived from that base class, so I can call a Render function. But I thought that CornedBee said that I could do that if the base class had a viritual function, and the derived classes where inheriting from it....
The two classes. cTree1 is derived from the cOnScreen class:
Code:
class cOnScreen{
public:
virtual void compute() = 0;
private:
int telle;
};
class cTree1: public cOnScreen{
public:
int total;
cTree1(){};
virtual void compute();
private:
};
Then I have a main class that makes me an object of cTree1, and then I am making a pointer to cOnscreen, and I want the cOnScreen pointer to point to the object made from cTree1....but the compiler says I can't do that like this. Error on the bol line.
Code:
int _tmain() {
cTree1 tre1 = new cTree1();
cOnScreen* screen = &tre1;
tre1->compute();
return 0;
}
Ohhh...myyy ****ing god...I am sooo stupid....what am I doing..when I posted the code and pressed submit I saw the most stupid error ever...just forget the whole thing...
I thought about taking it away when I saw it...so no one else could see it, but I changed my mind....guess I got a bit confused becuase I first wrote everything, and I got a warning about a bad end of file in my header file, then I struggeled with it in an hour, didn't know what was wrong. Then I copy pasted the whole **** to a new cpp, and h file, and then it compiled...so I guess the main function was written a "bit" fast..