#include <iostream>
class Database
{
public:
Database()
{
std::clog << "Database object created " <<std::endl ;
}
~Database()
{
std::clog << "Database object destroyed " << std::endl;
}
virtual void Open(const std::string & ) = 0 ;
} ;
class SqlServer : public Database
{
public:
void Open(const std::string & conn)
{
std::clog << "Attempting to open the connection "<< std::endl ;
}
~SqlServer()
{
std::clog << "SqlServer:Database object destroyed "<< std::endl ;
}
} ;
int main()
{
Database &ref = SqlServer();
ref.Open("uid=user;pwd=default");
return 0 ;
}
output
Database object created
Attempting to open the connection
SqlServer:Database object destroyed // why this get called as destructor is not virtual in database
Database object destroyed
Note: if I replace ref by pref then everything works fine i.e. sqlserver destructor won't get called.