Pointer to member variable of derivied class

Discussion in 'C++' started by g18c@hotmail.com, Aug 10, 2005.

  1. Guest

    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
    int var;

    class ChildClass : public BaseClass
    int var2;

    typedef int (BaseClass::*PInt);

    void dosomething(PInt p, BaseClass* pclass)
    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;


    // end code!

    Thanks in advance,

    , Aug 10, 2005
    1. Advertisements

  2. Frank Chang

    Frank Chang Guest

    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
    Frank Chang, Aug 10, 2005
    1. Advertisements

  3. Guest

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

    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?


    , Aug 10, 2005
  4. Frank Chang

    Frank Chang Guest

    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
    Frank Chang, Aug 10, 2005
  5. Frank Chang

    Frank Chang Guest

    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
    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.
    Frank Chang, Aug 10, 2005
  6. Guest

    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, 2005
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. slide_o_mix
    Oct 15, 2003
  2. Fraser Ross
    Fraser Ross
    Aug 14, 2004
  3. Stephen Howe
    Stephen Howe
    Nov 6, 2012
  4. somenath
    James Kanze
    Jul 2, 2013
  5. somenath
    Aug 29, 2013

Share This Page