forked from grayed/kmbo--21
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
42 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,6 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class A { | ||
string a_s; | ||
friend int main(); | ||
public: | ||
string getBString() const { /* TODO: return B::b_s value */; } | ||
}; | ||
class B : public A { | ||
string b_s; | ||
friend int main(); | ||
public: | ||
B() { b_s = "It's b!"; } | ||
}; | ||
|
||
int main() | ||
{ | ||
B b; | ||
A *a = &b; | ||
cout << "b address is " << &b << ", a_s address is " << &b.a_s << ", b_s address is " << &b.b_s << endl; | ||
cout << "b size is " << sizeof(b) << ", a_s size is " << sizeof(b.a_s) << endl; | ||
cout << "b_s is '" << a->getBString() << "'" << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <iostream> | ||
#include "memarith.h" | ||
|
||
using namespace std; | ||
|
||
int main() | ||
{ | ||
B b; | ||
A *a = &b; | ||
cout << "b address is " << &b << ", a_s address is " << &b.a_s << ", b_s address is " << &b.b_s << endl; | ||
cout << "b size is " << sizeof(b) << ", a_s size is " << sizeof(b.a_s) << endl; | ||
cout << "b_s is '" << a->getBString() << "'" << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <string> | ||
|
||
class A { | ||
std::string a_s; | ||
int foo; | ||
|
||
friend int main(); // for debugging display purposes | ||
|
||
public: | ||
std::string getBString() const { return *((const std::string *)(this + 1)); } | ||
float getBData(int idx) const { return ((const float *)((const std::string *)(this + 1)))[idx]; } | ||
}; | ||
|
||
class B : public A { | ||
std::string b_s; | ||
float data[7]; | ||
|
||
friend int main(); // for debugging display purposes | ||
|
||
public: | ||
B() { b_s = "It's b!"; } | ||
}; | ||
|
||
// | class B | ||
// | class A | self | | ||
// | a_s; foo | b_s | | ||
// адрес b_s = адрес b плюс смещение b_s относительно начала b = адрес b плюс размер A |