Problem using overridden methods
-
As i said i my first post casting will fail (for vector<MyClass>) there is no way to use MyClass1 (or any other derived functionality) because that part of your objects is lost. (this cast: MyClass1* pBase = dynamic_cast<MyClass1*>(&storage[i]); will fail for storage of type vector<MyClass>)
To use the overridden functions from that vector you need to use pointers into the vector, than the overridden functionality will work correctly and also the casting (of-course to the correct pointer type).
//also since MyClass is designed as base class don't forget the virtual destructor (for the case you call delete on a MyClass* that has the address of a dynamically allocated MyClass1 object)
-
Heh, since my last post I've come to realize what you've said to be very much true, however, in my actual code, as the objects created are being made in a function that ends before the call to the function that doSomething() gets called in is ran, the objects get deleted and the pointers are pointing to null memory. Is there any way to cast without using pointers that will work? Or should I just come up with a completely different structure for my code to run that doesn't involve this polymorphism?
Edit: Just realized what you pointed out in your previous post about the functionality being lost as the vector allocates memory of size MyClass and not either of the MyCClass classes. In any case, is there an easy solution here? Or do I just have to use 2 seperate vectors of type MyCClass1 and MyCClass2?
-
[quote author="Psycho_Path" date="1331568900"]Heh, since my last post I've come to realize what you've said to be very much true, however, in my actual code, as the objects created are being made in a function that ends before the call to the function that doSomething() gets called in is ran, the objects get deleted and the pointers are pointing to null memory. Is there any way to cast without using pointers that will work? Or should I just come up with a completely different structure for my code to run that doesn't involve this polymorphism?
Edit: Just realized what you pointed out in your previous post about the functionality being lost as the vector allocates memory of size MyClass and not either of the MyCClass classes. In any case, is there an easy solution here? Or do I just have to use 2 seperate vectors of type MyCClass1 and MyCClass2?[/quote]
I gave you solution in my first post:
@ void main(etc...)
{
vector<MyClass *> storage;
MyCClass1 *mcc1 = new MyCClass1();
MyCClass2 *mcc2 = new MyCClass2();
storage.push_back(mcc1);
storage.push_back(mcc2);
for (int i = 0; i < storage.size(); i++)
{
storage[i] ->doSomething();
}
}
@As you can see, you create objects using operator new(), so the pointers won't become invalid after exiting the function, where thery were created. From other hand you have to make destructor of base class (MyClass) virtual and explicity delete every object, when you've done:
@
foreach (MyClass *element,
storage) {
delete element;
}
@That's the classical way of creating a polymorphic behaviour. For more information try to read Bjarne Stroustrup C++ Programming Language, The (3rd Edition).
-
Thanks for your help and for re-posting what you'd already taken the time to post that I'd overlooked. Much appreciated.
-
Yes, point at Wilk . Polymorphism (with pointers) can help.
@
#include <iostream>
#include <vector>using namespace std;
class MyClass
{
public:
virtual void doSomething() const
{
cout << "MyClass::doSomething" << endl;
}
};class MyCClass1: public MyClass
{
public:
void doSomething() const
{
cout << "MyClass1::doSomething" << endl;
}
};class MyCClass2: public MyClass
{
public:
void doSomething() const
{
cout << "MyClass2::doSomething" << endl;
}
};/*
void writeall(vector<MyClass*>& s)
{for (int i = 0; i < s.size() ; i++) { s[i]->doSomething(); }
}
*/int main()
{
vector<MyClass*> storage;
MyCClass1* mcc1 = new MyCClass1();
MyCClass2* mcc2 = new MyCClass2();
storage.push_back(mcc1);
storage.push_back(mcc2);//writeall(storage); for (int i = 0; i < storage.size() ; i++) { storage[i]->doSomething(); } return 0;
}
@ -
[quote author="Zlatomir" date="1331641008"]broadpeak you forgot the virtual destructor for MyClass and also you forgot to delete the dynamic allocated memory.[/quote]
I have forgotten nothing :)
But in this very simple example I would have liked to avoid any complications: I focused only polymorphically behavior. Of course, you are right, in a bigger project we have to use virtual desctructor. -
The polymorphic parts were posted before, so i assumed you wanted to give a complete example... and the things i said you forgot are pretty important parts of the example.
Anyway i "vote" for you to edit the post and add the missing parts, i don't think that 4 lines of code would complicate the example.
-
I do really like being able to do this:
class MyObj : public SomeObj<-right click here....
go to refactor->insert virtual functions of base classes
which opens a wonderful window that displays EVERY child class of that object with a checkbox by each function. You can just check off each signature you want to override and be done with it in one go!
-
@osirisgothra said in Problem using overridden methods:
I do really like being able to do this:
which opens a wonderful window that
You could write a QtCreator extension/plugin and try to find out how to get your hand on the data you need.
displays EVERY child class of that object with a checkbox by each function. You can just check off each signature you want to override
Probably via the Code Model / Symbols.
If you wanna try it, start a new topic.
People who have more experience with such things than I do may be able to help you then.