How to get address of an instance
-
: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
-
: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
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
-
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
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
-
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
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
-
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
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() calledThe 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 aB
instance is constructed, the vtable is still the one of classA
. -- Daniel Lohmann http://www.losoft.de -
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() calledThe 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 aB
instance is constructed, the vtable is still the one of classA
. -- Daniel Lohmann http://www.losoft.deThat'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
-
: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
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
-
: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
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