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. How to get address of an instance

How to get address of an instance

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
8 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.
  • C Offline
    C Offline
    CSharpDavid
    wrote on last edited by
    #1

    :confused::confused:I have a class - CVariable In the constructor I want to transfer the address of that instance to a CVariableManager class so that I can iterate though all my CVariable instances. I use CVariableManager::Add(CVariable * pvar) and from the constructor add the CVariable m_VarMgr::Add(this) The trouble is that the this value is always the same value. I assume that it points to the vtable for the function addresss. How can I get a pointer to the data instance HHEELLPP!:confused::confused::confused: David

    C I A 3 Replies Last reply
    0
    • C CSharpDavid

      :confused::confused:I have a class - CVariable In the constructor I want to transfer the address of that instance to a CVariableManager class so that I can iterate though all my CVariable instances. I use CVariableManager::Add(CVariable * pvar) and from the constructor add the CVariable m_VarMgr::Add(this) The trouble is that the this value is always the same value. I assume that it points to the vtable for the function addresss. How can I get a pointer to the data instance HHEELLPP!:confused::confused::confused: David

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You'll find that your object does not actually exist until the constructor returns. This could well be part of your problem. I don't believe you can use 'this' inside a constructor. Christian come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002

      S D 2 Replies Last reply
      0
      • C Christian Graus

        You'll find that your object does not actually exist until the constructor returns. This could well be part of your problem. I don't believe you can use 'this' inside a constructor. Christian come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002

        S Offline
        S Offline
        soptest
        wrote on last edited by
        #3

        If you "don't believe" try this:

        class _A
        {
        public:
        _A()
        {
        m_this = this;
        };
        _A *m_this;
        void f()
        {
        printf("instance: %p\n",m_this);
        }
        };

        void main()
        {
        _A a1,a2, *pa1,*pa2 ;

        a1.f();
        a2.f();
        pa1 = new \_A();
        pa2 = new \_A();
        pa1->f();
        pa2->f();
        delete pa1;
        delete pa2;
        

        return;
        }

        soptest

        C 1 Reply Last reply
        0
        • S soptest

          If you "don't believe" try this:

          class _A
          {
          public:
          _A()
          {
          m_this = this;
          };
          _A *m_this;
          void f()
          {
          printf("instance: %p\n",m_this);
          }
          };

          void main()
          {
          _A a1,a2, *pa1,*pa2 ;

          a1.f();
          a2.f();
          pa1 = new \_A();
          pa2 = new \_A();
          pa1->f();
          pa2->f();
          delete pa1;
          delete pa2;
          

          return;
          }

          soptest

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Hmmm... well, I wonder what the rule was then ? I had some problem to do with assuming an item was existant in the constructor, when it was only after the constructor returned that it existed..... Christian come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002

          1 Reply Last reply
          0
          • C Christian Graus

            You'll find that your object does not actually exist until the constructor returns. This could well be part of your problem. I don't believe you can use 'this' inside a constructor. Christian come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002

            D Offline
            D Offline
            Daniel Lohmann
            wrote on last edited by
            #5

            Actually one can use this in the constructor. The address is valid and does not change any more - so you can safely store it or pass it to someone else. It depends on how you use it. You have to consider that all members you did not initialize already are still undefined. And virtual functions are not dereferenced as expected during constructor execution. While beeing constructed, the object behaves like "static" typed. All functions are executed as if the object is of the class which constructer is currently executed:

            class A {
            public:
            A() {
            vf();
            }

            virtual void vf() {
                printf("A::vf() called\\n");
            }
            

            };

            class B : public A {
            public:
            virtual void vf() {
            printf("B::vf() called\n");
            }
            };

            void main ()
            {
            A a; // create an instance of class A
            B b; // create an instance of class B
            }

            The output of this little program would be:

            A::vf() called
            A::vf() called

            The reason for this is, that constructers are called in order of the inheritance graph beginning with the most general base class(es). (The constructer of the base class(es) is always executed before the constructor of the derived class is entered.) And the very first thing a constructor does is to init the vtable. So at the point in time the A part of a B instance is constructed, the vtable is still the one of class A. -- Daniel Lohmann http://www.losoft.de

            C 1 Reply Last reply
            0
            • D Daniel Lohmann

              Actually one can use this in the constructor. The address is valid and does not change any more - so you can safely store it or pass it to someone else. It depends on how you use it. You have to consider that all members you did not initialize already are still undefined. And virtual functions are not dereferenced as expected during constructor execution. While beeing constructed, the object behaves like "static" typed. All functions are executed as if the object is of the class which constructer is currently executed:

              class A {
              public:
              A() {
              vf();
              }

              virtual void vf() {
                  printf("A::vf() called\\n");
              }
              

              };

              class B : public A {
              public:
              virtual void vf() {
              printf("B::vf() called\n");
              }
              };

              void main ()
              {
              A a; // create an instance of class A
              B b; // create an instance of class B
              }

              The output of this little program would be:

              A::vf() called
              A::vf() called

              The reason for this is, that constructers are called in order of the inheritance graph beginning with the most general base class(es). (The constructer of the base class(es) is always executed before the constructor of the derived class is entered.) And the very first thing a constructor does is to init the vtable. So at the point in time the A part of a B instance is constructed, the vtable is still the one of class A. -- Daniel Lohmann http://www.losoft.de

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              That's it - it was the virtual function problem that bit me before. Thanks. Christian come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002

              1 Reply Last reply
              0
              • C CSharpDavid

                :confused::confused:I have a class - CVariable In the constructor I want to transfer the address of that instance to a CVariableManager class so that I can iterate though all my CVariable instances. I use CVariableManager::Add(CVariable * pvar) and from the constructor add the CVariable m_VarMgr::Add(this) The trouble is that the this value is always the same value. I assume that it points to the vtable for the function addresss. How can I get a pointer to the data instance HHEELLPP!:confused::confused::confused: David

                I Offline
                I Offline
                includeh10
                wrote on last edited by
                #7

                u can do it, i am sure because i use the way often to pass an instance itself to manager (or whatever). i.g. class MyClass { public: MyClass() { mng.Add(this);//Manager mng; } }; if u don't beleave, simplify ur code to do a test. includeh10

                1 Reply Last reply
                0
                • C CSharpDavid

                  :confused::confused:I have a class - CVariable In the constructor I want to transfer the address of that instance to a CVariableManager class so that I can iterate though all my CVariable instances. I use CVariableManager::Add(CVariable * pvar) and from the constructor add the CVariable m_VarMgr::Add(this) The trouble is that the this value is always the same value. I assume that it points to the vtable for the function addresss. How can I get a pointer to the data instance HHEELLPP!:confused::confused::confused: David

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  It would be possible to sort of hide the constructor of the CVariable class, and to instead, have variables allocated by the CVariableManager class. Something like this maybe: class CVariableManager; class CVariable() { private: friend class CVariableManager; CVariable( /* params go here */ ); ~CVariable(); public: // Other stuff here. }; // Probably in another header file somewhere... class CVariableManager { public: CVariableManager(); ~CVariableManager(); CVariable * CreateVariable( /* params go here */ ); // Define this function to create a new variable, add it to the list, and return it. void DeleteVariable( CVariable * p_poVariable ); // Define this function to delete the given variable and remove it from the list. }; Then you can use the CVariableManager to actually manage the variable creation and deletion. Chris Richardson

                  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