Sign in to follow this  

Simple Pointer Misunderstanding.

This topic is 3868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently reading "Teach Yourself c++ in 21 days" and in one of the examples (in the pointer chapter of course) confuses me.
class Simplecat
{
 public:
        Simplecat();
        ~Simplecat();
       
        int Getage() const { return *itsage }
        int Setage(int age) { *itsage = age }

 private:
        int * itsage;
        int * itsweight;
};

 Simplecat::Simplecat()
 {
     itsage = new int(2);
     itsweight = new int(5);
 }
I get everything until I get down to the constructor. Mainly the "itsage = new int(2); Since itsage is a pointer shouldn't the line read "*itsage = new int(2)" instead?

Share this post


Link to post
Share on other sites
No.

Remember, Pointers point to memory addresses. When the constructor gets called, itsage currently doesnt point to a memory address.

So we do itsage = new int(2);

why not *itsage = new int(2) ?

with no *, you are "using" the memory address, so you are setting the address itsage points to in "itsage = new int(2)"

Maybe this will help...



int main()
{

int *pointer; //Declaring a pointer that points to a int

pointer = new int; // making it point to a memory address

*pointer = 2; //setting the value of the address it points to to 2

//you use * to access the value at the memory address it points to.

//this next piece does the same as above

int *pointer = new int(2);
//maybe you were getting confused with this




Hoped this helped :D

Share this post


Link to post
Share on other sites
Quote:
Original post by erissian
In other words, "new" returns the address of the new object.

Yes, in fact dynamicly allocating memory for one int is pretty much useles ;)
I think that this code can really confuse some people, the real use of "new" is allocating blocks of memory, for example:

int foo = 12;
int *array = new int[foo];
array[5] = 6;

Now we can have an array which size we didn't knew when program was starting;)
please note : i'm using "[]" operator not "()"

Share this post


Link to post
Share on other sites
Yes, the times you'll really need to use new and not new[] are pretty rare. I can only think of a few uses like making a linked list class (which you shouldn't do except as either a)a learning exercise after which you stop using it in place of std::list or b)you've determined from a lot of experience and code profiling that std::list is a bottleneck in your code and that if you implemented the list yourself you could do better which as a beginner probably won't be for quite awhile). Another place would be when you have a list of pointers (eg something like std::list<T*>) and each individual pointer in the list will point to an individual object (like you have list of Entity *'s which are base pointers to derived classes utilizing polymorphism). Even Then you should probably use a smart pointer class which will take care of the memory for you.

edit: I take it back, the times when you use new and new[] should be as little as possible. When in doubt use a library container like std::vector, std::list, std::deque, std::map, or std::set among others. Don't use raw pointers, use smart pointer classes to protect your pointers.

Share this post


Link to post
Share on other sites
Sign in to follow this