How to get value through inheritance, if I have array with objects from first object and second? I have array with type A in object B. And in main function I put element A and B, I want get value countFiles B with from array How do it? (Can't use stl containers, only array and pointers)
#include <iostream> class A { private: public: A(){}; A(int size) : size(size){}; int size; }; class B : public A { public: A *arr; int countFiles = 5; B(int size, int countFiles) { this->size = size; this->countFiles = countFiles; } }; int main() { B *kek = new B(0, 4); A element1(5); A element2(6); A element3(7); B lol(0, 3); kek->arr = new A[5]; kek->arr[0] = element1; kek->arr[1] = element2; kek->arr[2] = element3; kek->arr[4] = lol; for (int i = 0; i < 5; i++) { if (kek->arr[i].size == 0) { std::cout << ((B*)(&kek->arr[i]))->countFiles << std::endl; } } system("pause>nul"); return 0; }
should give 3 but it gives out -146198184
https://stackoverflow.com/questions/65835335/how-to-get-value-through-inheritance-if-i-have-array-with-objects-from-first-ob January 22, 2021 at 04:34AM
没有评论:
发表评论