• Advertisement
Sign in to follow this  

of OOP and C++

This topic is 4286 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

Hi, recently, i was making examples for myself about "oop in c++". my little code is below: CObject class:
class CObject{
      private:
              int value;
      public:
             CObject(int val);
             ~CObject();
             int getvalue();
};

CObject::CObject(int val){
                     cout << "CObject created! (" << val << ")" << endl;
                     value = val;
                     }
CObject::~CObject(){
                    cout << "CObject deleted! (" << value << ")" << endl;
                    }
int CObject::getvalue(){
    return value;
}

CObjectManager class:
class CObjectManager{
      private:
              vector<CObject> objlist;
      public:
             CObjectManager();
             ~CObjectManager();
             void update();
             void addObject(CObject obj);
             CObject getobject(int index);
};

void CObjectManager::update(){
                       for (int r; r<objlist.size(); r++){
                           cout << "Object" << r << "'s value is: " << objlist[r].getvalue() << endl;
                           }
                       }
                       
CObjectManager::CObjectManager(){
     cout << "CObjectManager created!" << endl;
     }

CObjectManager::~CObjectManager(){
     cout << "CObjectManager deleted!" << endl;
     }
     
void CObjectManager::addObject(CObject obj){
     objlist.push_back(obj);
     cout << "CObject added! (" << obj.getvalue() << ")" << endl;
     }
     
CObject CObjectManager::getobject(int index){
        return objlist[index];
}

and main() note that it is a console app:
int main(int argc, char *argv[])
{
    CObjectManager *man = new CObjectManager;
    CObject        *obj = new CObject(17);
    CObject        *obj2 = new CObject(21);
    
    man->addObject(*obj);    
    man->addObject(*obj2);
    //obj2 = &man->getobject(0);
    cout << obj->getvalue() << endl;
    man->update();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Output: CObjectManager created! CObject created! (17) CObject created! (21) CObject added! (17) CObject deleted! (17) CObject deleted! (17) CObject added! (21) CObject deleted! (21) 17 Object0's value is: 17 Object1's value is: 21 Press any key to continue . . . But when i comment this line
cout << obj->getvalue() << endl;
so,
//cout << obj->getvalue() << endl;
Output: CObjectManager created! CObject created! (17) CObject created! (21) CObject added! (17) CObject deleted! (17) CObject deleted! (17) CObject added! (21) CObject deleted! (21) Press any key to continue . . . 1- Why, man->update() didn't generate any line? 2- CObject created! (17), CObject deleted! (17) CObject deleted! (17) So, c++ runs ~CObject for more then a time? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
This is a really bad example.
None of the objects are being deleted (hmm the os may clean up but you don't)and you have memory leaks.

How so? well you are using an std::vector and this uses references. I was going to rewrite this code, but I'm sure someone else will do it:)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This is a really bad example.
None of the objects are being deleted (hmm not quite true. none of the dymanic objects which you create are deleted by you, but the os will clean up. The other objects which are deleted are due to the std::vector). You should also defined the copy constructor(tho its not needed here because it will do a shallow copy and its defined by default).

How so? well you are using an std::vector and this uses references. I was going to rewrite this code, but I'm sure someone else will do it:)

Share this post


Link to post
Share on other sites
When you pass an object by value, like you do here:
void addObject(CObject obj);
The compiler will make a copy of the object by calling the copy-constructor. vector also stores copies of objects and whenever it resizes itself will create new copies of all it's old contents into new storage and destroy the old copies.

So man->addObject(*obj); creates a copy of *obj in the addObject member function. The vector then makes a further copy of the object and stores it. The first copy is then automatically destroyed when the function exits, printing out the first of the "CObject deleted! (17)" messages. At this point you have two CObjects with value 17. One on the heap and pointed to by obj and one inside the vector.

Now you call man->addObject(*obj);. This makes a copy of *obj2. The vector then finds that it needs to make some more room before it can store a second element (growth is exponential, so it doesn't always have to do this) and makes a new copy of the first object in new storage and destroys the old copy, printing out the second "CObject deleted! (17)" message. The vector then makes a copy of the second object and stores it. The first copy of the second object is then automatically destroyed when the function exits, printing out the "CObject deleted! (21)" message. At this point you have two CObjects with value 17 (one on the heap and pointed to by obj and one inside the vector) and two CObjectss with value 21 (one on the heap and pointed to by obj2 and one inside the vector).

Since you never delete the objects you allocated with new these objects are never destroyed.

CObjectManager::update() contains the code for (int r; r<objlist.size(); r++). Since r is never given an initial value the behaviour is undefined. What typically happens in these cases is that r picks up some random value from memory. It just so happens that when you include the line cout << obj->getvalue() << endl; you happen to end up with a zero at the memory r gets its value from, whereas if you don't include the line you get some positive value greater than 2, and so the loop never runs. Always initialise variables.

Σnigma

Share this post


Link to post
Share on other sites
thank you :) actually i forgot to ask, what are the wrongs of this code :)

please write this code as right form for me :)

Share this post


Link to post
Share on other sites

This topic is 4286 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.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement