I am having trouble understanding/fixing a problem I am encountering in my first attempt to create a derived container from an abstract superclass.
It seems that I cannot access the derived class's data from a member function. Each time I attempt it, I get an incorrect value.
tuple.h
class container {
public :
virtual int get_element ( std::size_t index ) = 0 ;
} ;
class tuple : public container {
private :
int* arr_ptr { } ;
std::size_t arr_size{ } ;
public :
tuple ( ) : arr_ptr { nullptr }, arr_size{ 0 }
{ }
tuple ( std::initializer_list<int> setter_list ) :
arr_ptr{ new int [ setter_list.size() ] },
arr_size{ setter_list.size() }
{ }
int get_element ( std::size_t index ) override { return arr_ptr[index] ; }
} ;
The call to tuple's member function get_element() in main() is displayed below:
main.cpp
int main ( ) {
tuple t { 1, 2, 3 } ;
std::cout << t.get_element( 0 ) ;
return 0 ;
}
The incorrect output on my computer is:
-842150451
arr_ptris not in fact a pointer and cannot be initialised withnullptrand the second member has no name.arr_sizedeclared? Please provide a minimal reproducible example.