Can someone explain this code?

Discussion in 'C++' started by Angus, Jun 23, 2008.

  1. Angus

    Angus Guest

    Wondering how this code worked

    I have a base class called eg MyBase. It has a private member called
    myprivatemember which is of course not accessible from users of the
    class.

    But I suddenly need to update the variable myprivate and I am not
    allowed to edit the original source code so I create a new class,
    MyNewToAllowChange which implements a public function called
    SetMyPrivate which allows me to change the variable.

    I realise this is bad design etc but just bear with me.

    I don't implement operator= (or assign) and in my code I have an
    instance of the base object - MyBase.

    I need to update the myprivatemember and so the following functions
    have been provided which make it possible.

    inline MyNewToAllowChange* Mt(MyBase* thebase)
    {
    return static_cast<MyNewToAllowChange*>(thebase);
    }

    I can then do this sort of thing:

    (if my base object is called mybaseobj (a pointer))
    Mt(mybaseobj)->SetMyPrivate(whatever);

    MyBase
    MyNewToAllowChange

    How is this working? Is it a fairly standard technique?

    I can see that it is useful in certain circumstances, just not see
    before.
     
    Angus, Jun 23, 2008
    #1
    1. Advertising

  2. Angus

    Noah Roberts Guest

    Angus wrote:

    > How is this working? Is it a fairly standard technique?


    As far as I can tell...

    You've not provided enough information to answer that question. It's a
    mystery.
     
    Noah Roberts, Jun 23, 2008
    #2
    1. Advertising

  3. On 2008-06-23 17:47, Angus wrote:
    > Wondering how this code worked
    >
    > I have a base class called eg MyBase. It has a private member called
    > myprivatemember which is of course not accessible from users of the
    > class.
    >
    > But I suddenly need to update the variable myprivate and I am not
    > allowed to edit the original source code so I create a new class,
    > MyNewToAllowChange which implements a public function called
    > SetMyPrivate which allows me to change the variable.
    >
    > I realise this is bad design etc but just bear with me.
    >
    > I don't implement operator= (or assign) and in my code I have an
    > instance of the base object - MyBase.
    >
    > I need to update the myprivatemember and so the following functions
    > have been provided which make it possible.
    >
    > inline MyNewToAllowChange* Mt(MyBase* thebase)
    > {
    > return static_cast<MyNewToAllowChange*>(thebase);
    > }
    >
    > I can then do this sort of thing:
    >
    > (if my base object is called mybaseobj (a pointer))
    > Mt(mybaseobj)->SetMyPrivate(whatever);
    >
    > MyBase
    > MyNewToAllowChange
    >
    > How is this working?


    By praying to the gods, sacrificing virgins at full moon, and a few
    other tricks.

    At the very least you should used reinterpret_cast since static_cast is
    not allowed to do that conversion, and even if you did the result of the
    conversion is implementation defined. In short, while it might work for
    you now, if you try it on another platform and/or with another compiler
    you might be in for a surprise.

    --
    Erik Wikström
     
    Erik Wikström, Jun 23, 2008
    #3
  4. Angus wrote:
    > ...
    > I have a base class called eg MyBase. It has a private member called
    > myprivatemember which is of course not accessible from users of the
    > class.
    >
    > But I suddenly need to update the variable myprivate and I am not
    > allowed to edit the original source code so I create a new class,
    > MyNewToAllowChange which implements a public function called
    > SetMyPrivate which allows me to change the variable.


    Er... How??? If the member variable is indeed private and the class has
    no "friends", there's no way to gain access to it, regardless of how
    many "new" classes you introduce.

    > How is this working? Is it a fairly standard technique?


    Sorry, so far you haven't demonstrated any technique at all. Explain
    more clearly what "technique" exactly you are talking about.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 23, 2008
    #4
    1. Advertising

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. suzy
    Replies:
    3
    Views:
    450
  2. Replies:
    2
    Views:
    552
    ACE01
    Nov 7, 2006
  3. Aarti
    Replies:
    3
    Views:
    279
  4. Soumen banerjee

    Can someone please explain to me this code?

    Soumen banerjee, May 3, 2009, in forum: Python
    Replies:
    3
    Views:
    408
  5. Replies:
    1
    Views:
    86
    Evertjan.
    Apr 3, 2005
Loading...

Share This Page