Thread: Help Me Understand Destructor Function in Vector

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    22

    Help Me Understand Destructor Function in Vector

    myString is of type vector i get the constructor and I have struct Node, can someone help me visualize what happens in the destructor function. How does that work, it's one line of code but I'm not understanding how it works. What happens under the hood with that bit of code?
    myString.~vector<Node*>();



    Code:
    struct Node{
    string inf;   
    Node *prev;
    Node(const string &val, Node*ptr=0) 
    :inf(value),prev(ptr){}
    };
    
    private:
    vector<Node*>myString;
    
    
    Object::Object():myString(50),myCount(0)
    {
    }
    
    
    Object::~Object(){
       myString.~vector<Node*>(); 
       //Someone Help me understand this
    }
    
    

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should show the definition of the Object class. The code is probably wrong as it is unlikely that the destructor for myString should be explicitly involved in the destructor of the Object class.

    If the Object class is actually a mix-up with the Node class, i.e., by Object you mean Node, then this is definitely wrong: when a Node object is destroyed, the myString member will already be destroyed. If there is any work for the Node class destructor to do, then it likely has to be manually destroying the objects that the pointers in the vector point to, but whether this is true or not depends on whether you want the Node to own those other objects.
    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

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    22
    Hey laserlight, class object would be something like this. I removed all other uneccessary stuff.

    Code:
    classObject{
    
    public:
    Object();
    ~Object();
    //other function prototypes here
    private:
    vector<Node*>myString;
    Object*Last;
    Object*First;
    };
    


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Okay, here's your code with proper formatting:
    Code:
    classObject
    {
    public:
        Object();
        ~Object();
        //other function prototypes here
    private:
        vector<Node*> myString;
        Object*Last;
        Object*First;
    };
    
    Object::~Object()
    {
        myString.~vector<Node*>(); 
        //Someone Help me understand this
    }
    So, perhaps you have a main function like this:
    Code:
    int main()
    {
        Object x;
    }
    x is default constructed, so it has a vector<Node*> member variable named myString that is empty. At the end of the main function, x is destroyed, invoking its destructor. We see this line:
    Code:
    myString.~vector<Node*>();
    The above is an explicit invocation of the destructor for the member variable named myString, i.e., on that line, the member variable named myString is destroyed. It is good for this member variable to be destroyed as after all we are destroying the Object object that owns it, so we want to destroy its members too. The trouble is that this member variable will already be destroyed at the end of the Object destructor, hence this explicit invocation of the destructor for the member variable named myString is a mistake that results in an attempt to destroy the member variable twice, resulting in undefined behaviour.
    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. Replies: 3
    Last Post: 05-03-2015, 05:33 AM
  2. Replies: 3
    Last Post: 02-09-2015, 09:56 PM
  3. Replies: 4
    Last Post: 07-05-2013, 02:30 AM
  4. Replies: 2
    Last Post: 04-08-2012, 10:32 AM
  5. Replies: 13
    Last Post: 08-24-2006, 12:22 AM