Thread: C++ question (Classes and pointers)

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    C++ question (Classes and pointers)

    // Listing 8.5
    // Creating objects on the free store
    // using new and delete

    #include <iostream>

    class SimpleCat
    {
    public:
    SimpleCat();
    ~SimpleCat();
    private:
    int itsAge;
    };

    SimpleCat::SimpleCat()
    {
    std::cout << "Constructor called.\n";
    itsAge = 1;
    }

    SimpleCat::~SimpleCat()
    {
    std::cout << "Destructor called.\n";
    }

    int main()
    {
    std::cout << "SimpleCat Frisky...\n";
    SimpleCat Frisky;
    std::cout << "SimpleCat *pRags = new SimpleCat...\n";
    SimpleCat * pRags = new SimpleCat;
    std::cout << "delete pRags...\n";
    delete pRags;
    std::cout << "Exiting, watch Frisky go...\n";

    return 0;
    }
    /* My question is the following is *pRags a pointer for the the SimpleCat class or for the Object Frisky if it is for the Object please tell me why.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    pRags is a pointer that is used to store the address of an anonymous instance of the class SimpleCat allocated on the heap and has no connection with the Frisky instance.
    zen

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Thank you that's what I thought
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I have another question would * pRags be like an object on the heap?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    pRags de-referenced is an object on the heap, but pRags is a pointer allocated on the stack (that points to something on the heap).
    zen

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. Question on pointers in classes
    By swarm in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2002, 06:16 PM
  5. question time (classes and pointers)
    By swarm in forum C++ Programming
    Replies: 1
    Last Post: 02-20-2002, 11:33 AM