By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,849 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

dynamic class members workaround

P: n/a
If I'm not mistaken, C++ doesn't have support for dynamic class
members. I'm considering if such a facility would be useful, and what
method would make a good workaround.

I have a generic buffer class, which can have one or more cursor
classes attached to it. Right now, they are both separate classes,
and I use them as follows:

class Buffer {
class Cursor {

pBuffer = new Buffer;
pCursor = pBuffer->CreateCursor();

The parent buffer of pCursor is implicit at the Insert() call, when it
would preferably be explicit. A cleaner facility might be to create a
dynamic member for each cursor. Below, the parameter to CreateCursor
is the new name of the dynamic member, and the function knows the type
to which it must be initialized. The syntax
"pBuffer->pCursor->Insert()" would trigger an overridable lookup and
dispatch function in the buffer class, followed by the Insert()
function of the particular cursor instance.

pBuffer = new Buffer;

The only way I can think of to achieve this kind of behavior, short of
importing some specialized non-standard library, is to use handles, C
style. Statement (2) below was actually my original technique, but
then the cursor and buffer code was intermingled.

pBuffer = new Buffer;
HANDLE pCursor;
pCursor = pBuffer->CreateCursor();
(1) pBuffer->GetCursor(pCursor)->Insert();
(2) pBuffer->Insert(pCursor);

The drawback of the handle technique is that handles must be
maintained outside of the class, but it is fast because the handles
can be the indexes into the cursor class instance pointers array. I
suppose I could create a hash table for cursor names, and then refer
to them using the string name, like this:

pBuffer = new Buffer;

I like this solution, but there is performance overhead of using a
hash table to lookup the class instance for the given string at
runtime. Anyway, I'm just writing to clarify my thoughts. I will
probably use the split class method I outlined first just for its low
overhead. If you have any comments, feel free to reply.
Jul 22 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.