Results 1 to 18 of 18

Thread: What is actualy a pointer....[Resolved]

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    What is actualy a pointer....[Resolved]

    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.

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    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
    Have I helped you? Please Rate my posts.


  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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?)

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    OK..that explained a bit. But still not all. When you are making objects from classes does how does it know how many bits it has to read then.


    And if you are right then I guess this is not the way I have to go to try to remember what way the pointer workes when it comes to inheritense...

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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?

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Wrong. You can use a Shape* to point to a Circle or a Rectangle, but you can use only the functions they inherit from Shape.
    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.

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    OK. So I wasn't that wrong...just asked for too much. But still I can make a Rendering Queue the way I wan't with the way it works. Thanks.

    ØØ

  10. #10

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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;

  11. #11
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322
    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.

    small example of how to do this:

    Code:
    class employee  
    {
    public:
    	employee();
    	virtual ~employee();
    
    	virtual void print() const = 0;
    
    };
    
    
    
    #include "employee.h"
    #include <iostream.h>
    
    class worker : public employee
    {
    public:
    	worker();
    	virtual ~worker();
    	
    	virtual void print() const;
    };
    
    
    void worker::print() const
    {
    	cout << "HELLO FROM WORKER" << endl;
    }
    
    
    
    
    #include "worker.h"
    #include "employee.h"
    
    int main()
    {
    	employee * e;
    	worker w;
    
    	e= &w;
    
    	e->print();
    
    	return 0;
    }
    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

  12. #12
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322
    Well better yet, let me just include the project.
    Attached Files Attached Files
    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

  13. #13
    Hyperactive Member Maven's Avatar
    Join Date
    Feb 2003
    Location
    Greeneville, TN
    Posts
    322

    Re: What is actualy a pointer....

    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

  14. #14

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Re: What is actualy a pointer....

    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....

  15. #15

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    This is what I used:

    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;
    }

  16. #16

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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...

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    ****ing god is quite appropriate for that error
    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.

  18. #18

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width