Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active August 17, 2025 08:06
Show Gist options
  • Save sunmeat/3cfee2182aabece572a7ab0f3615dabc to your computer and use it in GitHub Desktop.
Save sunmeat/3cfee2182aabece572a7ab0f3615dabc to your computer and use it in GitHub Desktop.
будова об'єкта
#include <iostream>
#include <windows.h>
using namespace std;
class Dog {
public:
char* name; // 8 (якщо архітектура х64)
int age; // 4
void Guard() {
cout << "Dog::Guard()\n";
}
void Bark() {
cout << "Dog::Bark()\n";
}
static void Print() {
cout << "Dog::Print()\n";
}
};
int main() {
setlocale(0, "Ukrainian");
cout << "Розмiр в байтах типу Dog: ";
cout << sizeof(Dog) << "\n"; // 16
Dog d;
cout << "Адреса об'єкту d типу Dog: ";
cout << &d << " (" << (long long int) &d << ")\n";
cout << "Адреса першого поля об'єкта d: ";
cout << &d.name << " (" << (long long int) &d.name << ")\n";
cout << "Адреса другого поля об'єкта d: ";
cout << &d.age << " (" << (long long int) &d.age << ")\n";
cout << "Адреса статичного методу класу Dog: ";
cout << &Dog::Print << " (или ";
cout << &d.Print << ")\n";
cout << "Адреса звичайного методу класу Dog: ";
void (Dog:: * memfunc_ptr)() = &Dog::Bark;
auto cheat = &Dog::Bark;
void* method_address = *(void**)(&memfunc_ptr);
cout << method_address << "\n";
Dog* dog = new Dog;
dog->Bark();
(dog->*memfunc_ptr)();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment