Reputation: 311
First question on StackOverflow. And I want apologize for my bad English.
I'm actually developing a menu for a video game. I want clear the code, so I want use vector to call different functions (like charge a picture or modify the text)
here's the code :
Menu.hh :
class Menu
{
int _position
std::vector<Menu> _graph;
std::list<sf::Text> _text;
public:
//constructors and destructors
virtual std::list<sf::Text> &modifyText();
}
Menu.cpp
std::list<sf::Text> &modifyText()
{
std::cout << this->_position << std::endl;
this->_text = this->_graph[this->_position].modifyText();
return (this->_text); // don't care of the return
}
void Menu::initGame()
{
this->_graph.push_back(MenuBase());
// if i Put this code on the constructor, the constructor call himself and not the MenuBase constructor
this->modifyText();
}
MenuBase.hpp
class MenuBase : public Menu
{
//constructor and destructor
std::list<sf::Text &modifyText(){std::cout << "work" << std::endl;
//I've more code, but it's just initialization of text and the return.
//The work is here to show what's happen on the standard output.
}
The ouptut of this code is : 0, 0 then SegFault. I expect to see "0" then "work" on standard output. So, why the MenuBase function is not called ?
To have the full code, here's the gitHub repository : https://github.com/Aridjar/AW_like
Upvotes: 1
Views: 82
Reputation: 726919
What you see is called Object Slicing. When you put your MenuBase
objects in a vector container of Menu
objects, any functionality not present in the Base
class is "sliced off" your object; only the functionality of the base class remains. The behavior of your objects stops being polymorphic beyond the bounds of the class that you put in the container.
In order to keep the polymorphic behavior that you want, replace the container of objects with a container of pointers. To avoid headaches with manual memory management, prefer smart pointers to regular ones:
std::vector< std::shared_ptr<Menu> > _graph;
Upvotes: 8