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
102 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cmake_minimum_required (VERSION 3.8) | ||
|
||
add_executable (memhacks "memhacks.cpp" "memhacks.h") | ||
|
||
# TODO: Add tests and install targets if needed. |
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,66 @@ | ||
#include <iostream> | ||
#include "memhacks.h" | ||
|
||
using namespace std; | ||
|
||
// 1. Реализовать в A функцию-геттер, возвращающую значение B::b_s. | ||
// Используйте знание о расположении объектов в памяти. | ||
// | ||
// 2. Реализовать A::printData(), выводящую в поток текствое представление A::a_s, B::b_s и B::data | ||
// с помощью A::getBString() и аналогичных техник. | ||
// | ||
// 3. Реализовать A::printData2(), выводящую в поток текствое представление A::a_s, B::b_s и B::data | ||
// с помощью виртуальных функций. | ||
|
||
B::B() : b_s("It's b!") { | ||
for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++) | ||
data[i] = i * 2; | ||
} | ||
|
||
/// <summary> | ||
/// Выводит на экран адреса и размеры объекта типа <see cref="B"/> и его содержимого. | ||
/// Можно модифицировать для собственных отладочных целей. | ||
/// </summary> | ||
/// <param name="b">Изучаемый объект</param> | ||
void printInternals(const B& b) { | ||
const A* a = &b, * a2 = a + 1; | ||
cout << "Address of b is 0x" << &b << ", address of b.a_s is 0x" << &b.a_s << ", address of b.b_s is 0x" << &b.b_s << endl; | ||
cout << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << endl; | ||
cout << "B string is '" << b.getBString() << "'" << endl; | ||
//cout << "B data: "; b.printData(cout); cout << endl; | ||
} | ||
|
||
/// <summary> | ||
/// Извлекает значение <see cref="B::b_s"/> из текущего объекта. | ||
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>. | ||
/// </summary> | ||
/// <returns>Значение B::b_s</returns> | ||
std::string A::getBString() const { | ||
// TODO | ||
} | ||
|
||
/// <summary> | ||
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/> | ||
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток | ||
/// с помощью адресной арифметики. | ||
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>. | ||
/// </summary> | ||
void A::printData(std::ostream& os) { | ||
// TODO | ||
} | ||
|
||
/// <summary> | ||
/// Извлекает значения <see cref="A::a_s"/>, <see cref="B::b_s"/> и <see cref="B::data"/> | ||
/// из текущего объекта и выводит их в текстовом виде в указанный выходной поток | ||
/// с помощью виртуальных функций, предусмотренных в классе <see cref="A"/>. | ||
/// </summary> | ||
void A::printData2(std::ostream& os) { | ||
// TODO | ||
} | ||
|
||
int main() | ||
{ | ||
B b; | ||
printInternals(b); | ||
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,30 @@ | ||
#pragma once | ||
|
||
#include <ostream> | ||
#include <string> | ||
|
||
class B; // чтобы можно было объявить printInternals() как friend в классе A | ||
|
||
class A { | ||
std::string a_s; | ||
int foo; | ||
|
||
friend void printInternals(const B&); | ||
|
||
public: | ||
std::string getBString() const; | ||
void printData(std::ostream& os); | ||
void printData2(std::ostream& os); | ||
}; | ||
|
||
class B : public A { | ||
std::string b_s; | ||
float data[7]; | ||
|
||
friend void printInternals(const B&); | ||
|
||
public: | ||
B(); | ||
}; | ||
|
||
void printInternals(const B& b); |