R
Robert
In the code which follows, could someone let me know why the second
call to Print() calls the base class Print() rather than the derived
class Print()? I've started going through the C++ FAQs but haven't
found the answer to this one yet (though I have found that I should be
using standard library containers rather than arrays [FAQ 34.1]).
TBase.h:
#include <iostream>
class TBase
{
public:
TBase(void) {};
~TBase(void) {};
virtual void Print() const = 0 {std::cout << "Print from TBase" <<
std::endl; }
};
TDerived_A.h:
#include "TBase.h"
#include <iostream>
class TDerived_A : public TBase
{
public:
TDerived_A(void) {};
virtual void Print() const { std::cout << "Print from TDerived_A" <<
std::endl; }
~TDerived_A(void) {};
};
test.cpp:
#include "TBase.h"
#include "TDerived_A.h"
#include <iostream>
int main( void )
{
TDerived_A derivedA;
TBase *arrTBase_1[] = {&derivedA};
arrTBase_1[0]->Print();
TBase arrTBase_2[] = {TDerived_A()};
arrTBase_2[0].Print();
}
output:
Print from TDerived_A
Print from TBase
call to Print() calls the base class Print() rather than the derived
class Print()? I've started going through the C++ FAQs but haven't
found the answer to this one yet (though I have found that I should be
using standard library containers rather than arrays [FAQ 34.1]).
TBase.h:
#include <iostream>
class TBase
{
public:
TBase(void) {};
~TBase(void) {};
virtual void Print() const = 0 {std::cout << "Print from TBase" <<
std::endl; }
};
TDerived_A.h:
#include "TBase.h"
#include <iostream>
class TDerived_A : public TBase
{
public:
TDerived_A(void) {};
virtual void Print() const { std::cout << "Print from TDerived_A" <<
std::endl; }
~TDerived_A(void) {};
};
test.cpp:
#include "TBase.h"
#include "TDerived_A.h"
#include <iostream>
int main( void )
{
TDerived_A derivedA;
TBase *arrTBase_1[] = {&derivedA};
arrTBase_1[0]->Print();
TBase arrTBase_2[] = {TDerived_A()};
arrTBase_2[0].Print();
}
output:
Print from TDerived_A
Print from TBase