|
-
February 2nd, 2010, 10:58 AM
#1
delete doesn't appear to delete
The singleton class based off the CRTP is not provided, nonetheless consider.
Code:
# include <iostream>
# include <map>
# include <string>
# include <vector>
class Base {
public:
virtual ~Base() {}
};
class Derived : public Base {
public:
Derived() {}
~Derived() { std::cout << "o" << std::endl; }
};
enum TEST_KEYS { A, B, C, END_OF_KEYS };
class test : public singleton < test > {
typedef TEST_KEYS test_keys;
Base *key_list[ END_OF_KEYS ];
public :
template < typename ClassType >
test& insert ( test_keys const & key, ClassType * ptr ) {
if ( key_list [ key ] != 0 ) {
throw ( std::runtime_error( "key exists" ) );
}
key_list [ key ] = ptr;
return ( *this );
}
template < typename ClassType >
Derived& Get ( test_keys const & key ) {
if ( key_list [ key ] == 0 ) {
throw ( std::runtime_error( "no match" ) );
}
//I think static_cast here should suffice
return static_cast< ClassType& >( *( key_list [ key ] ) );
}
template < typename ClassType >
void Delete ( test_keys const & key ) {
if ( key_list [ key ] != 0 ) {
delete key_list[ key ];
}
}
};
//later
int main() {
for ( int odx ( 0 ) ; odx < 2; ++odx ) {
test::instance().insert < Derived >( A, new Derived () );
//now remove it...
test::instance().Delete < Derived > ( A ) ;
}
std::cin.get();
}
It appears that the derived class 'Derived' is not being 'Deleted' from the m_list even though it's clear from debugging and stream output that the destructor of the Derived class is called. That said, on the next call to insert (i.e when odx = 1) the exception 'key exists' is thrown. (I'll admit that after a handful of years with C++ I'm embarassed to admit I'm struggle with what appears to be the basics)
How do I resolve this?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|