473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pointer to member variable of derivied class

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

Aug 10 '05 #1
5 3791
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

Aug 10 '05 #2
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

Aug 10 '05 #3
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.

Aug 10 '05 #4
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.

Aug 10 '05 #5
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.

Aug 10 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
1
by: none | last post by:
Hi, I have a base class with a pointer-to-member function variable. Then I have a derived class that needs to use that variable to call a member function (with the same arguments and return value...
2
by: lou zion | last post by:
hi all, i've got a class that takes a parameterless function pointer as a parameter. i want to store that function pointer in a variable and i'm trying to figure out the syntax. i came up with...
7
by: andylcx | last post by:
Hi all, I have a question about the code below. Any idea abuot that, thanks in advance! myclass.h class myclass { public: myclass(); ~myclass(); void setdata();
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
9
by: Cyron | last post by:
Hello friends, Recently I have begun exploring the features that the STL map collection provides. While learning how it worked, I encountered a result that confused me. Up until now, I had...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.