1

I run to following program , note that the value of the map is a ref (ClassA&) -

#include <iostream>
#include <map>
using namespace std  ; 

class ClassA {
    public :
         ClassA () {    cout<<"Hay ! "<<endl ; }    
        ~ClassA () {    cout<<"Bye ! "<<endl ; }
} ; 

int main () { 
    map< string,ClassA& > myMap ; 
    ClassA a   ; 
    myMap.insert( pair<string,ClassA&>("A",a) ) ; 
    myMap.clear() ; 




}

And get output -

Hay ! 
Bye !

Seems like the myMap.clear() didn't affected cause there is no one more calling to ClassA destructor , can you explain me why ?

URL87
  • 10,667
  • 35
  • 107
  • 174

1 Answers1

8

Destructor is not called when a reference is deleted. Speaking of which.. I thought maps with references would be illegal

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87