Push_back (this) pushes an invalid vector pointer

I have a vector of UnderlyingClass pointers stored in another object, and inside the method in UnderlyingClass I want to add a "this" pointer to the end of this vector. When I look at the contents of a vector immediately after calling push_back, the wrong pointer is there. What could be wrong?

cout << "this: " << this << endl;
aTextBox.callbacks.push_back(this); 
cout << "size is " << aTextBox.callbacks.size() << endl;
cout << "size-1: " << aTextBox.callbacks[aTextBox.callbacks.size()-1] << endl;
cout << "back: " << aTextBox.callbacks.back() << endl;
cout << "0: " << aTextBox.callbacks[0] << endl;
cout << "this: " << this << endl;
cout << "text box ptr: " << &aTextBox << endl;
cout << "text box callbacks ptr: " << &(aTextBox.callbacks) << endl;

Here's the conclusion:

this: 0x11038f70
size is 1
size-1: 0x11038fa8
back: 0x11038fa8
0: 0x11038fa8
this: 0x11038f70
text box ptr: 0x11039070
text box callbacks ptr: 0x11039098

By the way, callbacks are a WebCallback pointer vector, and UnderlyingClass implements WebCallback:

std::vector<WebCallback*> callbacks;


class UnderlyingClass
    :public WebCallback 

Copied from comments: (see answer below)

exit:

this: 0x6359f70 
size is 1 
size-1: 0x6359fa8 
back: 0x6359fa8 
0: 0x6359fa8 
this: 0x6359f70 
WebCallback This: 0x6359fa8 
text box ptr: 0x635a070 
text box callbacks ptr: 0x635a098 

okay, so that explains why the pointers don't match.

So my real question is:

, ? , WebCallback , onWebCommand() , callbacks [0] → onWebCommand() onWebCommand(), UnderlyingClass .

+3
2

, :

class UnderlyingBase {
  char d[56];
};

class UnderlyingClass
    :public UnderlyingBase, 
     public WebCallback {

};

, . - , - , WebCallback*.

[UnderlyingBase]
 > char[56]: 56 bytes, offset 0x0

[WebCallback]
 > unknown:  x bytes, offset 0x0

[UnderlyingClass]
 > [UnderlyingBase]: 56 bytes (0x38 hex), offset 0x0
 > [WebCallback]:    x  bytes, offset 0x38

, WebCallback*, , - WebCallback, UnderlyingClass UnderlyingBase, 0x38 (56) .

+8

:

cout << "this: " << this << endl;
cout << "WebCallback This: " << dynamic_cast<WebCallback*>(this) << endl;

, , .

+3

Source: https://habr.com/ru/post/1714565/


All Articles