|
-
January 21st, 2003, 10:22 PM
#1
Hiding new/delete
Object creation/destruction.
I wonder if any of you c++ guru's could give me insight into a philosophical design issue. Forgive me if the answer is simple, I have an assembly background.
Consider the following...
class session
{
virtual void ProcessData() = 0;
};
class mysession : public session
{
public:
virtual void ProcessData() {}
};
class sessionlist
{
public:
session* NewSession()
{
return AddToList( new mysession() );
}
void RemoveSession( mysession *s )
{
RemoveFromList( s );
delete s;
}
session* AddToList( session* s ) { return s; }
void RemoveFromList( session* s ){}
};
class server
{
public:
sessionlist sl;
void OnAccept()
{
Accept( sl.NewSession() );
}
void OnDissconnect( session *s )
{
}
void Accept( session* s ) {}
};
It would be nice if I could add server, serverlist, and session to a library. All I would have to do is derive a class from session for my specific implementation then use server and serverlist as is. However, because new and delete must know the object type to properly create and destroy the object. In this case I must also use a special instance of session list. Just to call the correct version of new and delete! This type of thing comes up all the time for me. I can hide everything away except the creation and destruction of objects. What I actually do to get around it is define a function type such as...
typedef session* (*session_CreateSession)();
typedef session* (*session_DeleteSession)( void* ptr );
... then create static functions in 'mysession' and call a 'SetCreateDestroyFunctions()' inside of sessionlist. But I don't consider this to be very elegant.
So the question is, is there a nice elegant 'c++' way to handle this situation?
Thanks for any response.
- Robert
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
|