Thread: pointers and classes

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    pointers and classes

    Hello,

    I am testing some code. I have a mammal class that has a virtual function Speak. I have 4 derived classes that inherit from the mammal class. The derived classes each have a Speak function.

    Howevever, I am using the code below. And one thing I don't understand is this line.

    Many thanks for any explanations for this line.

    Code:
        //A mammal pointer that points to 4 mammal objects
        Mammal *animalArray = new Mammal[4];
    
        Mammal *animal;
        animal = new Dog;
    
        animalArray = animal; //Don't understand, shouldn't it be animalArray[0] = animal
        animalArray->Speak();

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    animalArray is just a pointer to the first element of the array, but animalArray[0] is the dereferenced first element and therefore isn't a pointer.
    You can assign pointers that are at the same level to each other (i.e. the same number of *'s)
    Since that code doesn't delete what's in animalArray before reassigning it, you have a memory leak.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And I'm guessing, based on inherited classes, that what you want is an array of pointers-to-Mammal? If you try to copy a Dog object into a Mammal object slot, you'll lose stuff; but a pointer-to-Mammal can hold a pointer-to-Dog.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    I believe you can also do:

    *animalArray[0] = animal;
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by execute View Post
    I believe you can also do:

    *animalArray[0] = animal;
    You could, but again you would lose anything "extra" in Dog that's not in Mammal (a la type slicing). This means that the presumed polymorphic call on the next line will fail.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your sample code isn't quite right, as has been hinted above.

    I'm pretty sure that what you want is something like this:
    Code:
        
        //A mammal array that hold pointers to 4 mammal objects
        Mammal *animalArray[4];
    
        Mammal *animal = new Dog;
    
        animalArray[0] = animal; //Don't understand, shouldn't it be animalArray[0] = animal
        animalArray[0]->Speak();
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
        //A mammal array that hold pointers to 4 mammal objects
        Mammal* animal = new Dog;
        animal->Speak();
        delete animal;
    Why not just this (and don't forget to delete what you allocate!)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    Thanks for all the response.

    I think where I was really getting confused was this line here.

    Code:
    Mammal *animalArray = new Mammal[4];
    I look at this line as: I am creating a single mammal pointer. That pointers to an array of mammal objects. Just want to check that my terminology is correct

    Thanks,

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Besides the leak, you are also very nearly treating an array as having polymorphic elements, and that isn't going to work.

    Soma

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by steve1_rm View Post
    Hello,

    Thanks for all the response.

    I think where I was really getting confused was this line here.

    Code:
    Mammal *animalArray = new Mammal[4];
    I look at this line as: I am creating a single mammal pointer. That pointers to an array of mammal objects. Just want to check that my terminology is correct

    Thanks,
    Yes, this is correct understanding.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q: Pointers to Classes '->' notation
    By Howie17 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2008, 10:09 AM
  2. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  3. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  4. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 02:03 PM
  5. Pointers to classes
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2002, 09:25 PM