Learning C ++. Now I pass OOP, in the task I need to work with dynamic memory. The problem is when I increment a variable like this:this->a[this->index++] == &aa; , this->index becomes equal to numbers like 6422016, 6422048 etc. When I do this: this->index++; this->a[this->index - 1] = &aa; this->index++; this->a[this->index - 1] = &aa; With a variable, everything is fine.
What is it connected with?
Compiler: MINGW 3.3
OS: win10
CMake: 3.6.2
GDB: 7.8
Job Code:
#include <iostream> #include <string> using namespace std; class Date { int year; public: Date(int y): year(y) {} ~Date() { cout << "Date destructor" << endl; } void setDate(int y) { this->year = y; } int getYear() { return this->year; } }; class A{ Date date; public: A(Date& d): date(d) {} void setDate(Date& d) { this->date = d; } Date* getDate(); }; Date* A::getDate() { Date* new_date = &this->date; return new_date; } class B { A *a[]; int index = 0; public: ~B() { delete[] a; } void addA(A& aa) { this->a[this->index++] = &aa; } // ΠΠ΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ void func(int); void func2(int); }; void B::func(int year) { int counter = 0; for(int i = 0; i < this->index; i++) { cout << i << endl; if(this->a[i]->getDate()->getYear() == year) { cout << "Yes " << year << endl; counter = i; } } func2(counter); } void B::func2(int a) { cout << this->a[a]->getDate()->getYear() << endl; } int main() { Date d(2000); A a(d), c(d); B b; b.addA(a); b.addA(c); b.func(2000); return 0; } Thank!