Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer to member variable of derivied class

Reply
Thread Tools

Pointer to member variable of derivied class

 
 
g18c@hotmail.com
Guest
Posts: n/a
 
      08-10-2005
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

 
Reply With Quote
 
 
 
 
Frank Chang
Guest
Posts: n/a
 
      08-10-2005
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

 
Reply With Quote
 
 
 
 
g18c@hotmail.com
Guest
Posts: n/a
 
      08-10-2005
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

 
Reply With Quote
 
Frank Chang
Guest
Posts: n/a
 
      08-10-2005
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.

 
Reply With Quote
 
Frank Chang
Guest
Posts: n/a
 
      08-10-2005
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++
--------------------------------------------------------------------------------

s11n (an abbreviation for serialization) is an Open Source project
focused on the generic serialization of objects (i.e., object
persistence) in the C++ programming language.

 
Reply With Quote
 
g18c@hotmail.com
Guest
Posts: n/a
 
      08-10-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Function pointer member variable to non-member function Alex C++ 0 10-15-2003 05:26 PM
Function pointer member variable to non-member function slide_o_mix C++ 0 10-15-2003 03:37 PM



Advertisments