Pointer to member variable of derivied class

G

g18c

I am trying to have a pointer to a member variable, however i will be
deriving a number of classes from a base class. Whilst the code below
works, i am wondering if there is a better way of doing this, or indeed
if there are any traps i am missing.

#include <iostream>

class BaseClass
{
public:
int var;
};

class ChildClass : public BaseClass
{
public:
int var2;
};

typedef int (BaseClass::*PInt);

void dosomething(PInt p, BaseClass* pclass)
{
(*pclass.*p)++;
cout << "value: " << *pclass.*p << endl;
}

void main()
{
BaseClass base;
ChildClass child;

base.var = 10;
child.var2 = 20;

PInt p = &BaseClass::var;
PInt p2 = (PInt)&ChildClass::var2;

dosomething(p,&base);
dosomething(p2,&child);
}

// end code!

Thanks in advance,

Chris
 
F

Frank Chang

There are hidden dangers to your use of pointers. Suppose I do a
static_cast<BaseClass*> or reinterpret cast<BaseClass*> on any random
pointer which does not point to your base or derived class. Then ,
suppose I pass the result of the static_cast or reinterpret_cast to
dosomething(,). You will find that you get a nasty run-time error
unless you handle this case in do something
 
G

g18c

Ok thanks for the reply Frank, nasty points aside, is this designed
correctly?

I appreciate your concern about giving dosomething(,) pointers to
random classes... i know it is possible and this is why i started to
think the design was wrong.

All this test code is for creating a serialisation class. I will store
a list of serialisable fields in a class. On serialisation, i plan to
pass in a pointer to the object to be used to dereference the member
variable pointers... this will save me having a list for each instance
(which i dont want to do). Not sure if there are any better ways of
doing this?

Cheers,

Chris
 
F

Frank Chang

Chris, In a previous life, my former software development manager asked
me to do a task very similar to yours in Microsoft Visual C++. I used
MFC's Serialization feature and the CArchive class. I found that a
document whose persistent data consists entirely of primitive data
types or serializable objects can often be serialized with a few lines
of code. Of course, if you are using LINUX, then the Microsoft MFC
Serialization architecture won't be able to help you. Perhaps, LINUX
already has serialization approach. I can help you locate it if you
wish.
 
F

Frank Chang

Chris, I just found a link http://s11n.net which is an open source
serialization framework for C++. It will work on UNIX, LINUX.. Windows
XP.
The reason we want to use a serialization framework is because the
serialization algorithms are complex as well as tedious. Why write your
own C++ serialization class when others have already done it for you.
Thank you.

Welcome to s11n.net!
Home of s11n: the serialization framework for C++
 
G

g18c

thanks for the link i have come accross that in the past (before i
started writing this project), along with the boost libaries too.
whilst s11n looked interesting i had a go and rolling my own, partly
for experience i guess!! almost got there, i have created a
writer/reader class which goes from vectors/strings/ints etc to binary
and vice versa. hooking up class fields to the writer/reader object is
my final task. ill have another closer look at those files, s11n
certainly looks better than the boost libaries imho as there are less
#defines required... maybe i can get some tips for my final hurdle.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top