Problem with polymorphism
-
i'm facing a problem with polymorphism currently and need help. i've 1 base class, let's call it Base, and 2 Derived class, Derive1 and Derive2. Base is NOT an abstract class. on top of these 2, i've a container class Container that contains a vector of Base pointers. (i.e. vector < Base* > pointer) now in Container, there's a function called init() that will initialize the vector of pointers and according to the type of class it belongs to. e.g. pointer[0] = *(new Base); pointer[1] = *(new Derive1); pointer[2] = *(new Derive2); and so on... the type of classes are created correctly when the constructors are called. however, when i tried to access the pointers again later in my program, the program only treats each pointer as Base and no longer as any derived classes. thus polymorphism cannot work at all... this is crucial to me as i have several classes that's supposed to belong to the vector of pointers. if i use a simple array polymorphism works. but i need the vector.... please help!!
-
i'm facing a problem with polymorphism currently and need help. i've 1 base class, let's call it Base, and 2 Derived class, Derive1 and Derive2. Base is NOT an abstract class. on top of these 2, i've a container class Container that contains a vector of Base pointers. (i.e. vector < Base* > pointer) now in Container, there's a function called init() that will initialize the vector of pointers and according to the type of class it belongs to. e.g. pointer[0] = *(new Base); pointer[1] = *(new Derive1); pointer[2] = *(new Derive2); and so on... the type of classes are created correctly when the constructors are called. however, when i tried to access the pointers again later in my program, the program only treats each pointer as Base and no longer as any derived classes. thus polymorphism cannot work at all... this is crucial to me as i have several classes that's supposed to belong to the vector of pointers. if i use a simple array polymorphism works. but i need the vector.... please help!!
ng kok chuan wrote: vector < Base* > pointer ng kok chuan wrote: pointer[0] = *(new Base); Are you sure this actually compiles? Shouldn't the second line actually be
pointer[0] = new Base;
etc...There's no reason why a vector can't be used here.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
ng kok chuan wrote: vector < Base* > pointer ng kok chuan wrote: pointer[0] = *(new Base); Are you sure this actually compiles? Shouldn't the second line actually be
pointer[0] = new Base;
etc...There's no reason why a vector can't be used here.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
i was trying to simplify the problem here, which is why i used *(new Base); what i really have is a while loop: while (!odyn.IsEOF()) { node.push_back(*(new MazeNode)); node[node.size()-1].init((string)eqpid, (string)eqptyp, 1); odyn.MoveNext(); } and node.push_back() takes a reference, which is why i need to dereference the memory allocated. the code compiles with no problem, and works without error too. however polymorphism does not work at all. i've tested using a Base array in main() and polymorphism works.
-
ng kok chuan wrote: vector < Base* > pointer ng kok chuan wrote: pointer[0] = *(new Base); Are you sure this actually compiles? Shouldn't the second line actually be
pointer[0] = new Base;
etc...There's no reason why a vector can't be used here.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
hi, anyone please help?? i really need to be able to do both polymorphism and use vectors.
-
hi, anyone please help?? i really need to be able to do both polymorphism and use vectors.
There is no problem using polymorphism in a vector. You are probably doing something wrong elsewhere. Try to post the code of the declaration of the vector, the piece of code in which you add new elements to it and the piece of code where you make use of these elements (and where it seems polymorphism doesn't work). This will help us to help you :-D
-
i'm facing a problem with polymorphism currently and need help. i've 1 base class, let's call it Base, and 2 Derived class, Derive1 and Derive2. Base is NOT an abstract class. on top of these 2, i've a container class Container that contains a vector of Base pointers. (i.e. vector < Base* > pointer) now in Container, there's a function called init() that will initialize the vector of pointers and according to the type of class it belongs to. e.g. pointer[0] = *(new Base); pointer[1] = *(new Derive1); pointer[2] = *(new Derive2); and so on... the type of classes are created correctly when the constructors are called. however, when i tried to access the pointers again later in my program, the program only treats each pointer as Base and no longer as any derived classes. thus polymorphism cannot work at all... this is crucial to me as i have several classes that's supposed to belong to the vector of pointers. if i use a simple array polymorphism works. but i need the vector.... please help!!
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)
class Base { public: int m_iInt; public: int virtual GetInt() {return m_iInt); CString virtual GetClassInfo() {return CString("Base")}; int NonVirtualGetInt(){return m_iInt); } class Derive1 : Base { public: int m_iAnotherInt; public: int virtual GetInt() {return m_iAnotherInt); CString virtual GetClassInfo() {return CString("Derive1")}; int NonVirtualGetInt(){return m_iAnotherInt); } class Derive2 : Base { public: int virtual GetInt() {return 2 * m_iInt); CString virtual GetClassInfo() {return CString("Derive1")}; int NonVirtualGetInt(){return 2 * m_iInt); } int main() { Base *pointer[3]; pointer[0] = new Base; pointer[1] = new Derive1; pointer[2] = new Derive2; pointer[0]->m_iInt = 1 // access Base.m_iInt pointer[1]->m_iInt = 10 // access Base.m_iInt although p1 is a Derive1 (CDerive1*)pointer[1]->m_iAnotherInt = 15 // access Derive1.m_iAnotherInt (CDerive2*)pointer[2]->m_iInt = 100 (CDerive2*)pointer[2]->m_iAnotherInt = 150 // won't work, Derive2 has not m_iAnotherInt for (i=0; i<3; i++) cout << pointer[i]->GetInt() // will write 1 (Base.GetInt(), 15 (Derive1.GetInt), 200 (Derive2.GetInt as GetInt is virtual for (i=0; i<3; i++) cout << pointer[i]->m_iInt // 1, 10, 100, all classe contain m_iInt, but C++ think it's all Base.m_iInt for (i=0; i<3; i++) cout << pointer[i]->m_iAnotherInt // won't work, m_iAnotherInt not member of Base, and C++ thinks all pointers are Base* for (i=0; i<3; i++) cout << pointer[i]->NonVirtualGetInt() // returns 1, 10, 100 as Base.NonVirtualInt() is called in all three cases for (i=0; i<3; i++) { CString cs=pointer[i]->GetClassInfo() if (cs == "Base") pointer[i]->NonVirtualGetInt() // Base.NonVirtualGetInt() if (cs == "Derive1") (Derive1*)pointer[i]->NonVirtualGetInt() // Derive1.NonVirtualGetInt() if (cs == "Derive2") (Derive2*)pointer[i]->NonVirtualGetInt() // Derive2.NonVirutalGetInt() }
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 B -
i was trying to simplify the problem here, which is why i used *(new Base); what i really have is a while loop: while (!odyn.IsEOF()) { node.push_back(*(new MazeNode)); node[node.size()-1].init((string)eqpid, (string)eqptyp, 1); odyn.MoveNext(); } and node.push_back() takes a reference, which is why i need to dereference the memory allocated. the code compiles with no problem, and works without error too. however polymorphism does not work at all. i've tested using a Base array in main() and polymorphism works.
ng kok chuan wrote: and node.push_back() takes a reference If it does, then your vector is not a vector of pointers, and that is the prolem. If you want polymorphism to work, your vector will have to store pointers, not objects.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
i was trying to simplify the problem here, which is why i used *(new Base); what i really have is a while loop: while (!odyn.IsEOF()) { node.push_back(*(new MazeNode)); node[node.size()-1].init((string)eqpid, (string)eqptyp, 1); odyn.MoveNext(); } and node.push_back() takes a reference, which is why i need to dereference the memory allocated. the code compiles with no problem, and works without error too. however polymorphism does not work at all. i've tested using a Base array in main() and polymorphism works.
ng kok chuan wrote: was trying to simplify the problem here, which is why i used *(new Base); When posting, always copy/paste your code verbatim. Re-typing (or simplifying when it is not obvious you are doing so) into the forum will, more often than not, lead to erroneous assumptions and misguided answers.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
ng kok chuan wrote: and node.push_back() takes a reference If it does, then your vector is not a vector of pointers, and that is the prolem. If you want polymorphism to work, your vector will have to store pointers, not objects.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
ok i've got the problem solved. silly me. as you said, i required a vector of node pointers, but when i first started off, i only used a vector of nodes. it worked perfectly fine till i tried to use polymorphism, and this problem arised. now i've made all the necessary changes and it's ok now. thanks for the reply :)
-
i'm facing a problem with polymorphism currently and need help. i've 1 base class, let's call it Base, and 2 Derived class, Derive1 and Derive2. Base is NOT an abstract class. on top of these 2, i've a container class Container that contains a vector of Base pointers. (i.e. vector < Base* > pointer) now in Container, there's a function called init() that will initialize the vector of pointers and according to the type of class it belongs to. e.g. pointer[0] = *(new Base); pointer[1] = *(new Derive1); pointer[2] = *(new Derive2); and so on... the type of classes are created correctly when the constructors are called. however, when i tried to access the pointers again later in my program, the program only treats each pointer as Base and no longer as any derived classes. thus polymorphism cannot work at all... this is crucial to me as i have several classes that's supposed to belong to the vector of pointers. if i use a simple array polymorphism works. but i need the vector.... please help!!
ng kok chuan wrote:
pointer[0] = *(new Base); pointer[1] = *(new Derive1); pointer[2] = *(new Derive2); and so on...
This what is known a late binding ng kok chuan wrote: the type of classes are created correctly when the constructors are called. however, when i tried to access the pointers again later in my program, the program only treats each pointer as Base and no longer as any derived classes. thus polymorphism cannot work at all... this is crucial to me as i have several classes that's supposed to belong to the vector of pointers. if i use a simple array polymorphism works. but i need the vector. How about putting the virtual before the name of the function (in the base class) ng kok chuan wrote:vector < Base* > pointer) pointer[0] = *(new Base);
doesn't make sense Surely, it shold be pointer[0] = new Base; Anyway, I got a sample app to give you an idea. If you e-mail I'll gladly send it to you. Happy programming Alton