J
Jack
I have a class called "Base". This class has a protected member
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.
I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.
I pass an array of "Base" classes as a parameter to a function.The
individual items in this array may or may not be subclasses of the
"Base" class. Within this function, the member function
"GetBaseMember" is called, and the value of "m_base" is displayed to
the screen.
Intuitively, the output should always be "1", but unfortunately it's
not. The output alternates between "1" and "2". I don't understand why
this is. How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?
#include <iostream>
using namespace std;
class Base
{
protected:
int m_base;
public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};
class Derived : public Base
{
protected:
int m_derived;
public:
Derived() : Base(), m_derived( 2 ){}
};
void Foo( int cItems, Base b[] )
{
int i = 0;
for ( i = 0; i < cItems; i++ )
{
// I want the output to always be 1,
// but it alternates between 1 and 2
cout << "m_base = " << b.GetBaseMember() << endl;
}
}
int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];
Foo( NUM_ITEMS, d );
return 0;
}
variable "m_base" which can be retrieved using the public member
function "GetBaseMember". "m_base" is initialized to "1" and is never
changed.
I have another class which is a subclass of the "Base" class called
"Derived". This derived class has a member variable called
"m_derived". "m_derived" is initialized to "2" and is never changed.
I pass an array of "Base" classes as a parameter to a function.The
individual items in this array may or may not be subclasses of the
"Base" class. Within this function, the member function
"GetBaseMember" is called, and the value of "m_base" is displayed to
the screen.
Intuitively, the output should always be "1", but unfortunately it's
not. The output alternates between "1" and "2". I don't understand why
this is. How else can I pass this "subclassable" class array to a
function and retrieve the expected base class member variable? What
concept am I missing here?
#include <iostream>
using namespace std;
class Base
{
protected:
int m_base;
public:
Base() : m_base( 1 ){}
int GetBaseMember() { return m_base; }
};
class Derived : public Base
{
protected:
int m_derived;
public:
Derived() : Base(), m_derived( 2 ){}
};
void Foo( int cItems, Base b[] )
{
int i = 0;
for ( i = 0; i < cItems; i++ )
{
// I want the output to always be 1,
// but it alternates between 1 and 2
cout << "m_base = " << b.GetBaseMember() << endl;
}
}
int main()
{
const int NUM_ITEMS = 4;
Derived d[NUM_ITEMS];
Foo( NUM_ITEMS, d );
return 0;
}