Click here to Skip to main content
15,893,904 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Problem with polymorphism Pin
Ryan Binns2-Mar-05 17:17
Ryan Binns2-Mar-05 17:17 
GeneralRe: Problem with polymorphism Pin
ng kok chuan2-Mar-05 17:23
ng kok chuan2-Mar-05 17:23 
GeneralRe: Problem with polymorphism Pin
Ryan Binns2-Mar-05 22:27
Ryan Binns2-Mar-05 22:27 
GeneralRe: Problem with polymorphism Pin
ng kok chuan3-Mar-05 14:31
ng kok chuan3-Mar-05 14:31 
GeneralRe: Problem with polymorphism Pin
David Crow3-Mar-05 2:35
David Crow3-Mar-05 2:35 
GeneralRe: Problem with polymorphism Pin
ng kok chuan2-Mar-05 20:01
ng kok chuan2-Mar-05 20:01 
GeneralRe: Problem with polymorphism Pin
Cedric Moonen2-Mar-05 20:36
Cedric Moonen2-Mar-05 20:36 
GeneralRe: Problem with polymorphism Pin
Steen Krogsgaard2-Mar-05 21:40
Steen Krogsgaard2-Mar-05 21:40 
Your methods have to be virtual for polymorphism to work. If you need member data access to be polymorphic, either use access methods or cast your pointer from the array to the correct type using some kind of type information extracted from the pointed-to object using virtual methods.

Did that make any sense? If not, here's an example (lot of stuff leaved out, don't expect it to compile)
<br />
<br />
class Base<br />
{<br />
public:<br />
int m_iInt;<br />
<br />
public:<br />
int virtual GetInt() {return m_iInt);<br />
CString virtual GetClassInfo() {return CString("Base")};<br />
int NonVirtualGetInt(){return m_iInt);<br />
}<br />
<br />
class Derive1 : Base<br />
{<br />
public: <br />
int m_iAnotherInt;<br />
<br />
public:<br />
int virtual GetInt() {return m_iAnotherInt);<br />
CString virtual GetClassInfo() {return CString("Derive1")};<br />
int NonVirtualGetInt(){return m_iAnotherInt);<br />
}<br />
<br />
class Derive2 : Base<br />
{<br />
<br />
public:<br />
int virtual GetInt() {return 2 * m_iInt);<br />
CString virtual GetClassInfo() {return CString("Derive1")};<br />
int NonVirtualGetInt(){return 2 * m_iInt);<br />
}<br />
<br />
int main()<br />
{<br />
Base *pointer[3];<br />
<br />
pointer[0] = new Base;<br />
pointer[1] = new Derive1;<br />
pointer[2] = new Derive2;<br />
<br />
pointer[0]->m_iInt = 1  // access Base.m_iInt<br />
pointer[1]->m_iInt = 10  // access Base.m_iInt although p1 is a Derive1<br />
(CDerive1*)pointer[1]->m_iAnotherInt = 15  // access Derive1.m_iAnotherInt<br />
(CDerive2*)pointer[2]->m_iInt = 100<br />
(CDerive2*)pointer[2]->m_iAnotherInt = 150 // won't work, Derive2 has not m_iAnotherInt<br />
<br />
for (i=0; i<3; i++) <br />
   cout << pointer[i]->GetInt()  // will write 1 (Base.GetInt(), 15 (Derive1.GetInt), 200 (Derive2.GetInt as GetInt is virtual<br />
<br />
for (i=0; i<3; i++)<br />
   cout << pointer[i]->m_iInt // 1, 10, 100, all classe contain m_iInt, but C++ think it's all Base.m_iInt<br />
<br />
for (i=0; i<3; i++)<br />
   cout << pointer[i]->m_iAnotherInt // won't work, m_iAnotherInt not member of Base, and C++ thinks all pointers are Base*<br />
<br />
for (i=0; i<3; i++)<br />
   cout << pointer[i]->NonVirtualGetInt() // returns 1, 10, 100 as Base.NonVirtualInt() is called in all three cases<br />
<br />
for (i=0; i<3; i++) {<br />
   CString cs=pointer[i]->GetClassInfo()<br />
   if (cs == "Base") pointer[i]->NonVirtualGetInt() // Base.NonVirtualGetInt()<br />
   if (cs == "Derive1") (Derive1*)pointer[i]->NonVirtualGetInt() // Derive1.NonVirtualGetInt()<br />
   if (cs == "Derive2") (Derive2*)pointer[i]->NonVirtualGetInt() // Derive2.NonVirutalGetInt()<br />
}<br />


I'm sure you get the general idea. One thing that helps me in these cases is to always keep in mind that the compiler can only act on what it knows - so if it thinks an object is of type Base it will call Base-methods, unless the methods are virtual





Cheers
Steen.

"To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"
GeneralRe: Problem with polymorphism Pin
Alton Williams5-Mar-05 4:03
Alton Williams5-Mar-05 4:03 
GeneralC++ Wrapper for libCURL Pin
a_diwakar2-Mar-05 17:02
a_diwakar2-Mar-05 17:02 
Questionhow to use a video codec to compress avi file Pin
Mohsen Saad2-Mar-05 15:22
Mohsen Saad2-Mar-05 15:22 
GeneralText to matrix conversion Pin
SeanSheehan2-Mar-05 14:32
SeanSheehan2-Mar-05 14:32 
GeneralSmall executables Pin
Verolix2-Mar-05 13:49
Verolix2-Mar-05 13:49 
GeneralRe: Small executables Pin
ThatsAlok2-Mar-05 18:00
ThatsAlok2-Mar-05 18:00 
Generalresizing workarea Pin
ahto1112-Mar-05 12:07
ahto1112-Mar-05 12:07 
GeneralC++ library Pin
Ahmed Adel2-Mar-05 11:33
Ahmed Adel2-Mar-05 11:33 
GeneralRe: C++ library Pin
ThatsAlok2-Mar-05 18:10
ThatsAlok2-Mar-05 18:10 
QuestionError? Pin
Larsson2-Mar-05 10:53
Larsson2-Mar-05 10:53 
AnswerRe: Error? Pin
Ravi Bhavnani2-Mar-05 10:55
professionalRavi Bhavnani2-Mar-05 10:55 
GeneralRe: Error? Pin
Larsson2-Mar-05 10:57
Larsson2-Mar-05 10:57 
GeneralRe: Error? Pin
Ravi Bhavnani2-Mar-05 11:01
professionalRavi Bhavnani2-Mar-05 11:01 
GeneralRe: Error? Pin
Larsson2-Mar-05 11:06
Larsson2-Mar-05 11:06 
GeneralRe: Error? Pin
Christian Graus2-Mar-05 11:07
protectorChristian Graus2-Mar-05 11:07 
GeneralRe: Error? Pin
Larsson2-Mar-05 11:10
Larsson2-Mar-05 11:10 
GeneralRe: Error? Pin
Christian Graus2-Mar-05 11:13
protectorChristian Graus2-Mar-05 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.