Thread: Aggregation

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    9

    Question Aggregation

    I dont understand why cDept did not destroy pTeacher?

    Code:
    #include <string>
    using namespace std;
     
    class Teacher
    {
    private:
        string m_strName;
    public:
        Teacher(string strName)
            : m_strName(strName)
        {
        }
     
        string GetName() { return m_strName; }
    };
     
    class Department
    {
    private:
        Teacher *m_pcTeacher; // This dept holds only one teacher
     
    public:
        Department(Teacher *pcTeacher=NULL)
            : m_pcTeacher(pcTeacher)
        {
        }
    };
     
    int main()
    {
        // Create a teacher outside the scope of the Department
        Teacher *pTeacher = new Teacher("Bob"); // create a teacher
        {
            // Create a department and use the constructor parameter to pass
            // the teacher to it.
            Department cDept(pTeacher);
     
        } // cDept goes out of scope here and is destroyed
     
        // pTeacher still exists here because cDept did not destroy it
    
        delete pTeacher;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,721
    Quote Originally Posted by 4c0
    I dont understand why cDept did not destroy pTeacher?
    It destroyed m_pcTeacher, but it did not destroy was the object that m_pcTeacher pointed to. If you did want that Teacher object to be destroyed, then you should write a destructor, and also write (or disable) the copy constructor and copy assignment operator, or simply store an object or smart pointer instead of that pointer. Of course, in such a case the code in main would need to change.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Aggregation question
    By SterlingM in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2009, 11:30 PM
  2. Aggregation (sort of),
    By Terran in forum C++ Programming
    Replies: 13
    Last Post: 06-09-2008, 02:59 AM
  3. help in aggregation implementation
    By Bargi in forum C++ Programming
    Replies: 0
    Last Post: 11-01-2007, 03:01 AM
  4. problem with the has-a relationship (aggregation)
    By geo in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2005, 05:13 PM
  5. difference between aggregation and composition
    By anil_beloved in forum C Programming
    Replies: 1
    Last Post: 10-18-2004, 12:17 PM