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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A teaser

A teaser

Scheduled Pinned Locked Moved C / C++ / MFC
question
20 Posts 7 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.
  • A Alton Williams

    Hi all, Consider this class

    (unit1. h)
    class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };

    in the main program

    #include"unit1.h"
    int main()
    {
        MyClass myObject(300);
    }
    

    The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?

    V Offline
    V Offline
    Viorel
    wrote on last edited by
    #7

    You can add to the total number of ways the following unusual one:

    *(int*)myObject = 400;
    

    (This is because the MyClass class does not have virtual functions, and m_nNum is the first and only member).

    A 1 Reply Last reply
    0
    • V Viorel

      You can add to the total number of ways the following unusual one:

      *(int*)myObject = 400;
      

      (This is because the MyClass class does not have virtual functions, and m_nNum is the first and only member).

      A Offline
      A Offline
      Alton Williams
      wrote on last edited by
      #8

      Viorel. wrote:

      You can add to the total number of ways the following unusual one:

      Good, I want more.

      V T 2 Replies Last reply
      0
      • A Alton Williams

        Hi all, Consider this class

        (unit1. h)
        class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };

        in the main program

        #include"unit1.h"
        int main()
        {
            MyClass myObject(300);
        }
        

        The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?

        S Offline
        S Offline
        Sarath C
        wrote on last edited by
        #9

        Im not sure my answer is correct or not. we can change the attribute value by following methods 1. Set value inside the destructor 2. Through a Member pointer SaRath.
        "Don't Do Different things... Do Things Differently..."

        T 1 Reply Last reply
        0
        • A Alton Williams

          Viorel. wrote:

          You can add to the total number of ways the following unusual one:

          Good, I want more.

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #10

          A similar unusual way is:

          int x = 400;
          memcpy(&myObject, &x, sizeof(x));
          

          One more way:

          class MyClass2
          {
          public:
             int m_nNum;
          };
          
          //
          // in main:
          //
          MyClass2 & myObject2 = (MyClass2&)myObject;
          myObject2.m_nNum = 400;
          
          T 1 Reply Last reply
          0
          • A Alton Williams

            Viorel. wrote:

            You can add to the total number of ways the following unusual one:

            Good, I want more.

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #11

            i hope you understand how bad the answers to you question can be, because of bad programming practices... this is the only reason why i didn't answered you with such code sample, because i advise you seriously to never use such samples in your code... security reason is always the best ;):cool:


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            1 Reply Last reply
            0
            • A Alton Williams

              Hi all, Consider this class

              (unit1. h)
              class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };

              in the main program

              #include"unit1.h"
              int main()
              {
                  MyClass myObject(300);
              }
              

              The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #12

              If anybody thinks of anything more horrible than this, let me know ;P

              #define private public
              #include"unit1.h"
              int main()
              {
              MyClass myObject(300);
              myObject.m_nNum = 400;
              }


              My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

              T 1 Reply Last reply
              0
              • A Alton Williams

                Hi all, Consider this class

                (unit1. h)
                class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };

                in the main program

                #include"unit1.h"
                int main()
                {
                    MyClass myObject(300);
                }
                

                The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #13

                And now to give a serious answer:

                #include"unit1.h"
                int main()
                {
                MyClass myObject(300);
                MyClass temp(400);
                myObject = temp;
                }


                My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                1 Reply Last reply
                0
                • V Viorel

                  A similar unusual way is:

                  int x = 400;
                  memcpy(&myObject, &x, sizeof(x));
                  

                  One more way:

                  class MyClass2
                  {
                  public:
                     int m_nNum;
                  };
                  
                  //
                  // in main:
                  //
                  MyClass2 & myObject2 = (MyClass2&)myObject;
                  myObject2.m_nNum = 400;
                  
                  T Offline
                  T Offline
                  ThatsAlok
                  wrote on last edited by
                  #14

                  Viorel. wrote:

                  public: int m_nNum; }; // // in main: // MyClass2 & myObject2 = (MyClass2&)myObject; myObject2.m_nNum = 400;

                  I will prefer get and set function rather that public variable outside the class

                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                  cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                  1 Reply Last reply
                  0
                  • S Sarath C

                    Im not sure my answer is correct or not. we can change the attribute value by following methods 1. Set value inside the destructor 2. Through a Member pointer SaRath.
                    "Don't Do Different things... Do Things Differently..."

                    T Offline
                    T Offline
                    ThatsAlok
                    wrote on last edited by
                    #15

                    SaRath C wrote:

                    1. Set value inside the destructor

                    If i set the value inside the destrutor, then how that value will  be persist!

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                    T 1 Reply Last reply
                    0
                    • T ThatsAlok

                      SaRath C wrote:

                      1. Set value inside the destructor

                      If i set the value inside the destrutor, then how that value will  be persist!

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #16

                      ThatsAlok wrote:

                      If i set the value inside the destrutor, then how that value will be persist!

                      actually, a destructor is a function like another. the only implicit call with it is when the object looses focus. but it is exactly like when some tries to delete this in the constructor. it is not really the appropriate place for that. a ctor is mostly to initialize the object state, and the dtor is mostly for releasing memoring that could have been allocated in the object life ; no more, no less, but not the opposite ! :doh:


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      T 1 Reply Last reply
                      0
                      • N Nemanja Trifunovic

                        If anybody thinks of anything more horrible than this, let me know ;P

                        #define private public
                        #include"unit1.h"
                        int main()
                        {
                        MyClass myObject(300);
                        myObject.m_nNum = 400;
                        }


                        My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #17

                        Nemanja Trifunovic wrote:

                        #define private public

                        ahemm, i'm not really sure about the level of this guy (and all the other around, reading), so is it really a good thing to point out that horror ? working though ! lol


                        TOXCCT >>> GEII power

                        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                        1 Reply Last reply
                        0
                        • T toxcct

                          ThatsAlok wrote:

                          If i set the value inside the destrutor, then how that value will be persist!

                          actually, a destructor is a function like another. the only implicit call with it is when the object looses focus. but it is exactly like when some tries to delete this in the constructor. it is not really the appropriate place for that. a ctor is mostly to initialize the object state, and the dtor is mostly for releasing memoring that could have been allocated in the object life ; no more, no less, but not the opposite ! :doh:


                          TOXCCT >>> GEII power

                          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                          T Offline
                          T Offline
                          ThatsAlok
                          wrote on last edited by
                          #18

                          toxcct wrote:

                          the only implicit call with it is when the object looses focus.

                          Rather says Losses focus pernamentaly!

                          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                          1 Reply Last reply
                          0
                          • A Alton Williams

                            Hi all, Consider this class

                            (unit1. h)
                            class MyClass{ public: MyClass(){}; MyClass(int nNum): m_nNum(nNum){} ~MyClass(){}; private: int m_nNum; };

                            in the main program

                            #include"unit1.h"
                            int main()
                            {
                                MyClass myObject(300);
                            }
                            

                            The question how many ways to modify the property (myObject.m_nNum) to another number without using setters or friends?

                            K Offline
                            K Offline
                            knoxplusplus
                            wrote on last edited by
                            #19

                            :mad:Hi !!! Can i ask you something? Why do you want to access private members an other way than getters & setters. For what do you use an class if you want that the members are visible for everybody. I thing you should use getters & setters (if you use classes) to modify a member-variables of the class. Don't use friend accessor. By !!! -:KNOX:-

                            T 1 Reply Last reply
                            0
                            • K knoxplusplus

                              :mad:Hi !!! Can i ask you something? Why do you want to access private members an other way than getters & setters. For what do you use an class if you want that the members are visible for everybody. I thing you should use getters & setters (if you use classes) to modify a member-variables of the class. Don't use friend accessor. By !!! -:KNOX:-

                              T Offline
                              T Offline
                              toxcct
                              wrote on last edited by
                              #20

                              hi knox, what you said was justified from your point of view i believe. however, you're still young (in the sense that you still have to learn about others), and shouldn't get angry just because you don't understand the OP point of interrest. there are other reason we access to private members avoiding getters/setters/friends that you certainly didn't have in mind when answered. also, don't be so categorical when saying not to use friend accessor. it is very useful in some particuliar case, and Mr. Stroustrup was far from being stupid when he defined this keywork in the C++ language definition... sincerely,


                              TOXCCT >>> GEII power

                              [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                              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