Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to modify private member variable of any class

How to modify private member variable of any class

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Neo Andreson

    How to modify private member variable of any class without using member function??? Is it possible? If Yes, How??? ThankXXXXXXXXXxx

    N Offline
    N Offline
    Nishad S
    wrote on last edited by
    #3

    If that class declares your function or class as a friend, then you can access. I think you are asking not for that. If so you just cast the class object to BYTE* and you can access the memory. Or, if you are aware the class structure then you can create a dummy class with all the member variables as public and can cast the actual object to that of this dummy class. Thus you can access the members.

    - NS -

    N 1 Reply Last reply
    0
    • C Cedric Moonen

      :~ You mean, from an external class ? What would be the use of private, protected and public keywords then ? What are you trying to do ?


      Cédric Moonen Software developer
      Charting control [v1.2]

      N Offline
      N Offline
      Neo Andreson
      wrote on last edited by
      #4

      Thanks for your kind reply. Ya then there is no meaning of private access specifier but i heard abt it that though it is violation of object oriented concepts but still it is possible in C++ using pointers. I am not trying to do anything great but just trying it out and i believe it is possible. -- modified at 4:59 Wednesday 31st October, 2007

      1 Reply Last reply
      0
      • N Neo Andreson

        How to modify private member variable of any class without using member function??? Is it possible? If Yes, How??? ThankXXXXXXXXXxx

        F Offline
        F Offline
        followait
        wrote on last edited by
        #5

        It may be compiler dependent, however, you can calculate the memeber address first, and access it directly (not through the object, do something like writing memory).;)

        N C 2 Replies Last reply
        0
        • N Neo Andreson

          How to modify private member variable of any class without using member function??? Is it possible? If Yes, How??? ThankXXXXXXXXXxx

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #6

          Usually, you souldn't. Anyway, you can modify a class private member variable inside a friend function (or friend operator, or a method of a friend class ). :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • N Nishad S

            If that class declares your function or class as a friend, then you can access. I think you are asking not for that. If so you just cast the class object to BYTE* and you can access the memory. Or, if you are aware the class structure then you can create a dummy class with all the member variables as public and can cast the actual object to that of this dummy class. Thus you can access the members.

            - NS -

            N Offline
            N Offline
            Neo Andreson
            wrote on last edited by
            #7

            Ya i got it, without using friend function,i did it.. i used typecasted it to char* thanKXXXXXXXXXXx

            N 1 Reply Last reply
            0
            • N Neo Andreson

              Ya i got it, without using friend function,i did it.. i used typecasted it to char* thanKXXXXXXXXXXx

              N Offline
              N Offline
              Nishad S
              wrote on last edited by
              #8

              Have a nice crack day... ;)

              - NS -

              N 1 Reply Last reply
              0
              • F followait

                It may be compiler dependent, however, you can calculate the memeber address first, and access it directly (not through the object, do something like writing memory).;)

                N Offline
                N Offline
                Neo Andreson
                wrote on last edited by
                #9

                ThankXXXXXXXXXXx Yes..i do not know it is compiler dependent or not,but using ur second suggestion it is possible.. ThankXXXXXXXXx a lot.

                1 Reply Last reply
                0
                • N Nishad S

                  Have a nice crack day... ;)

                  - NS -

                  N Offline
                  N Offline
                  Neo Andreson
                  wrote on last edited by
                  #10

                  Thank You!!!!!!!!!!!!!

                  1 Reply Last reply
                  0
                  • F followait

                    It may be compiler dependent, however, you can calculate the memeber address first, and access it directly (not through the object, do something like writing memory).;)

                    C Offline
                    C Offline
                    codeII
                    wrote on last edited by
                    #11

                    Here is a dirty trick to do so:

                    class ClassProtected
                    {
                    private:
                    int m_One;
                    UINT m_Two;
                    int m_Three;
                    };

                    class ClassPublic
                    {
                    public:
                    int m_One;
                    UINT m_Two;
                    int m_Three;
                    };

                    ClassProtected test; ClassPublic* pTest = (ClassPublic*) &test; pTest->m_One = 1; pTest->m_Two = 2; pTest->m_Three = 3;

                    F 1 Reply Last reply
                    0
                    • C codeII

                      Here is a dirty trick to do so:

                      class ClassProtected
                      {
                      private:
                      int m_One;
                      UINT m_Two;
                      int m_Three;
                      };

                      class ClassPublic
                      {
                      public:
                      int m_One;
                      UINT m_Two;
                      int m_Three;
                      };

                      ClassProtected test; ClassPublic* pTest = (ClassPublic*) &test; pTest->m_One = 1; pTest->m_Two = 2; pTest->m_Three = 3;

                      F Offline
                      F Offline
                      followait
                      wrote on last edited by
                      #12

                      I think it's a better way.:-D

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups