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 ?
Destructor is not called when a reference is deleted. Speaking of which.. I thought maps with references would be illegal