Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

.vscode
2 changes: 1 addition & 1 deletion electricity/electricity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ int main()
// TODO: создать цепь из генератора, выключателя и светильника

return 0;
}
}
2 changes: 1 addition & 1 deletion electricity/electricity.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,4 @@ class Switch : public Object {

// TODO: класс светильника с двумя полюсами

// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
// TODO: класс генератора с тремя полюсами (фаза, нейтраль, земпя).
43 changes: 31 additions & 12 deletions memhacks/memhacks.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <iostream>
#include "memhacks.h"

using namespace std;
A::A() : foo(42), a_s("It's a!") {}

B::B() : b_s("It's b!") {
for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++)
data[i] = i * 2;
for (auto i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
data[i] = i*2;
}
}

/// <summary>
Expand All @@ -15,19 +16,19 @@ B::B() : b_s("It's b!") {
/// <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;
std::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 << std::endl;
std::cout << "Size of A is " << sizeof(A) << ", size of B is " << sizeof(B) << std::endl;
std::cout << "B string is '" << b.getBString() << "'" << std::endl;
std::cout << "B data: "; const_cast<B*>(&b)->printData2(std::cout); std::cout << std::endl;
}

/// <summary>
/// Извлекает значение <see cref="B::b_s"/> из текущего объекта.
/// Извлекает значение <see cref="B ::b_s"/> из текущего объекта.
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
/// <returns>Значение B::b_s</returns>
std::string A::getBString() const {
// TODO
return * (( const std::string * ) (this + 1));
}

/// <summary>
Expand All @@ -37,7 +38,16 @@ std::string A::getBString() const {
/// Подразумевается, что текущий объект на самом деле представлено классом <see cref="B"/>.
/// </summary>
void A::printData(std::ostream& os) {
// TODO
os << "a_s is " << a_s << std::endl;
os << "b_s is " << getBString() << std::endl;

const float* bData = ( (const float * ) ( (( const std::string * ) (this + 1)) + 1) );
os << "data is: ";
for (size_t i = 0; i < 7; i++)
{
os << bData[i] << " ";
}
os << std::endl;
}

/// <summary>
Expand All @@ -46,12 +56,21 @@ void A::printData(std::ostream& os) {
/// с помощью виртуальных функций, предусмотренных в классе <see cref="A"/>.
/// </summary>
void A::printData2(std::ostream& os) {
// TODO
os << "a_s is " << a_s << std::endl;
os << "b_s is " << *getBStr() << std::endl;

const float* bData = getBData();
os << "data is: ";
for (size_t i = 0; i < 7; i++)
{
os << bData[i] << " ";
}
os << std::endl;
}

int main()
{
B b;
printInternals(b);
return 0;
}
}
13 changes: 10 additions & 3 deletions memhacks/memhacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ class A {
std::string a_s;
int foo;

friend void printInternals(const B&);
friend void printInternals( B&);

public:
A();
std::string getBString() const;
void printData(std::ostream& os);
void printData2(std::ostream& os);

virtual const std::string* getBStr() const = 0;
virtual const float* getBData() const = 0;
};

class B : public A {
std::string b_s;
float data[7];

friend void printInternals(const B&);
friend void printInternals( B&);

public:
B();

virtual const std::string* getBStr() const { return &b_s; }
virtual const float* getBData() const { return data; }
};

void printInternals(const B& b);
void printInternals( B& b);
2 changes: 1 addition & 1 deletion memhacks/newhacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ using namespace std;
int main()
{
return 0;
}
}
1 change: 0 additions & 1 deletion memhacks/newhacks.h
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#pragma once

2 changes: 1 addition & 1 deletion vectors/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ int main(int argc, char** argv) {
printf("address of v1.data[2000]: 0x%p, size: %zu bytes\n", &v1.data[2000], sizeof(v1.data[2000]));

return 0;
}
}
2 changes: 1 addition & 1 deletion vectors/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class vector3d {
friend int main(int argc, char** argv);
};

std::ostream& operator <<(std::ostream& os, const vector3d& v);
std::ostream& operator <<(std::ostream& os, const vector3d& v);