OOP in C++. How to drop the states - c++

I am writing a simple graphics editor.
There are 3 buttons on the panel, by pressing which I draw a square, circle or line.
There are 3 button handlers that change the state and 3 mouse event handlers in the class responsible for drawing the workspace.
void Cpr111View::OnCirc()
{
state = 1;
}
void Cpr111View::OnLine()
{
state = 2;
}
void Cpr111View::OnRect()
{
state = 3;
}
To shorten the question, I will give only one handler out of 3.
void Cpr111View::OnMouseMove(UINT nFlags, CPoint point)
{
if (state==2)
{
int oldmode;
CClientDC *pDC = new CClientDC(this);
if (nFlags && MK_LBUTTON)
{
oldmode = pDC->GetROP2();
pDC->SetROP2(R2_NOT);
pDC->MoveTo(begin.x, begin.y);
pDC->LineTo(oldmouse.x, oldmouse.y);
pDC->MoveTo(begin.x, begin.y);
pDC->LineTo(point.x, point.y);
oldmouse = point;
pDC->SetROP2(oldmode);
CView::OnMouseMove(nFlags, point);
}
}
if (state == 1)
{
….
}
if (state == 3)
{
….
}
void Cpr111View::OnLButtonUp(UINT nFlags, CPoint point)
{
}
void Cpr111View::OnLButtonDown(UINT nFlags, CPoint point)
{
}
Here is a drawing system.
I want to do it without states. That is, create an abstract class Figure. With three virtual methods per render:
Class Figure
{
public:
void virtual MouseMove()=0;
void virtual ButtonUp()=0;
void virtual ButtonDown()=0;
}
And from him in the classes of figures to override these methods.
Class Recatngle:public Figure
{
public:
void MouceMove() override;
...
}
Then, when the button is clicked, create an object of the corresponding class, then the button handler will look like this:
void Cpr111View::OnRect()
{
figure = new Rectangle();
}
And when drawing, the mouse handler will simply call the method of the corresponding class:
void Cpr111View::OnMouseMove(UINT nFlags, CPoint point)
{
figure - > MouseMove();
}
In order for figure to be available in two different methods, we declare it in the class:
class Cpr111View : public CView
{
public:
Figure figure;
…
}
This is how I want to do it, but the problem is that it can't be done that way. At a minimum, you cannot declare an abstract class variable. Then what type should it be if I am going to write a pointer to different classes into it? How to implement this architecture correctly, or maybe there are better ideas?

Using this way of polymorphic calls in C++ requires to use reference sematics.
I advise to read about it. E.g.: Reference and Value Semantics
So it class Cpr111View, you have to keep your Figure member by pointer, or by refernce.
In order to avoid having to manually manage the object, you should use a smart pointer like std::unique_ptr (or std::shared_ptr if you need to share ownership):
#include <memory> // for std::unique_ptr
class Cpr111View : public CView
{
public:
std::unique_ptr<Figure> figure;
//…
}
Of course you will need to allocate it before using it.
Instead of:
figure = new Rectangle();
use:
figure = std::make_unique<Rectangle>();
The method calls stay the same as in your code, e.g.:
figure->MouseMove();
If you not familiar with smart pointers in C++, I recomend to read about it. E.g.: What is a smart pointer and when should I use one?.

Related

Fixing an object oriented wrapper I am creating for bindbc.sfml

I am trying to create object oriented wrappers around bindbc.sfml, this is because I don't like the C-style syntax of CSFML.
The C-style syntax is not right -- in my opinion -- for an object oriented language. Dealing with pointers all the time is also unsafe.
This is not to say that CSFML isn't good -- it's great, and I've made some apps using bindbc-sfml. I just want to extend it to my liking with object oriented wrappers that can more closely match the C++ SFML syntax.
For the wrappers, I created a Shape class. This Shape class is seen in the original C++ SFML implementation:
class Shape : Transformable, Drawable {
void setTexture(sfTexture* texture, bool resetRect) {
ptr.sfShape_setTexture(texture, resetRect);
}
void setTextureRect(IntRect rect) {
ptr.sfShape_setTextureRect(rect.to_sfIntRect());
}
void setFillColor(Color color) {
ptr.sfShape_setFillColor(color.to_sfColor());
}
void setOutlineColor(Color color) {
ptr.sfShape_setOutlineColor(color.to_sfColor());
}
void setOutlineThickness(float thickness) {
ptr.sfShape_setOutlineThickness(thickness);
}
const(sfTexture)* getTexture() {
return ptr.sfShape_getTexture();
}
IntRect getTextureRect() {
return ptr.sfShape_getTextureRect().toIntRect();
}
Color getFillColor() {
return ptr.sfShape_getFillColor().toColor();
}
Color getOutlineColor() {
return ptr.sfShape_getOutlineColor().toColor();
}
float getOutlineThickness() {
return ptr.sfShape_getOutlineThickness();
}
size_t getPointCount() nothrow {
return ptr.sfShape_getPointCount();
}
Vector2f getPoint(size_t index) nothrow {
return ptr.sfShape_getPoint(index).toVector2f_noThrow();
}
FloatRect getLocalBounds() {
return ptr.sfShape_getLocalBounds().toFloatRect();
}
FloatRect getGlobalBounds() {
return ptr.sfShape_getGlobalBounds().toFloatRect();
}
private sfShape* ptr;
}
The sfShape pointer isn't currently initialized, I'll get to that issue soon.
As you can see, Shape extends the Transformable class and the Drawable interface. This again roughly matches what's seen in SFML. SFML.NET also did a similar wrapper for their CSFML C# bindings. What's great about SFML.NET is that you don't even know that you're using CSFML, this is because it feels just like C++ SFML.
Now, I will create a RectangleShape which will be a subclass of the Shape class:
(Btw I took a lot of inspiration from SFML.NET when it comes to these wrappers.)
class RectangleShape : Shape {
this(Vector2f size) {
_size = size;
setSize(_size);
}
Vector2f getSize() {
return _size;
}
void setSize(Vector2f size) {
_size = size;
}
override {
size_t getPointCount() {
return 4;
}
Vector2f getPoint(size_t index) {
final switch (index) {
case 0:
return Vector2f(0, 0);
case 1:
return Vector2f(_size.x, 0);
case 2:
return Vector2f(_size.x, _size.y);
case 3:
return Vector2f(0, _size.y);
}
}
}
private Vector2f _size;
}
As you can see, the Rectangle class only overrides the getPointCount and getPoint methods.
These are the methods that the superclass - Shape - will use to construct the shape object for it to actually be drawable.
Now, let us add the following code to the Shape class so that we can construct a Shape via these two methods, which we assume that the child provides us a good implementation for:
class Shape : Transformable, Drawable {
this() {
ptr = sfShape_create(&getPointCount, &getPoint, cast(void*)this);
}
extern(C) private static ulong getPointCount(void* data) nothrow {
return (cast(Shape)data).getPointCount();
}
extern(C) private static sfVector2f getPoint(size_t index, void* data) nothrow {
return (cast(Shape)data).getPoint(index).to_sfVector2f_noThrow();
}
I hear you asking, what's going on here?
We are providing two callbacks to the getPointCount and getPoint methods via function pointers, and we're passing in the current object to the data void* pointer. It's kind of hard to understand, but if you read through it carefully you should get a rough idea of what's going on.
Now, when we create a new instance of Rectangle, I will assume that the constructor will be called, the sf_shape ptr will be initialized correctly (as it will be utilizing the crucial getPoint and getPointCount methods) and everything will be OK.
This is the following test code I had:
void main() {
loadSFML();
RectangleShape rectangleShape = new RectangleShape(Vector2f(50, 50));
rectangleShape.setPosition(Vector2f(50, 50));
rectangleShape.setFillColor(Color.Blue);
RenderWindow renderWindow = new RenderWindow(sfVideoMode(500, 500), "Tests", sfWindowStyle.sfDefaultStyle, null);
sfEvent event;
while (renderWindow.isOpen()) {
while (renderWindow.pollEvent(&event)) {
if (event.type == sfEventType.sfEvtClosed) {
renderWindow.close();
}
}
renderWindow.clear(Color.Yellow);
renderWindow.ptr.sfRenderWindow_drawShape(rectangleShape.ptr, null);
renderWindow.display();
}
}
I would read through this line by line to get a good idea of what's going on.
Really, for demonstration purposes, we're using the renderWindow's ptr variable for drawing. When I can get this to work I will create wrapper functions so that it's nicer to use, but for now it's not important.
What I'd expect to pop up on screen is a 50x50 rectangle, filled with a blue color, at the position 50x50 on the screen.
Upon running the application, I don't see anything -- it's just a yellow screen.
I am very confused why this is the case, it seems like I've done everything fine, but I've obviously made a mistake somewhere in my implementation. I don't know specifically if it's an issue on my end, or a bug in bindbc-sfml, but this issue has infuriated me, because I am not getting what I expected to show up on screen.
Fixed it by calling sfShape_update here:
class RectangleShape : Shape {
this(Vector2f size) {
_size = size;
setSize(_size);
ptr.sfShape_update();
}

What is the meaning of "It is possible to map objects in the problem domain to those in the program"?

I have read the above statement in the OOP C++ balagurusamy book. This statement is written under the topic of "benefits of OOP." I have tried to understand this but i am not getting. so can anyone help me to sort out this?
This means that you can associate a relationship(kind of) between real life objects and objects in a program. For example,
EXAMPLE 1
Lets say you have a Factory of different Vehicles and the vehicles have different attributes like name, mfd date, number of tires, size etc etc.
Now you may(can) implement this real life problem(scenario) in a program as follows:
class Vehicle
{
...constructors and other code here
std::string name;
float date;
std::string color;
...//and so on
}
class Car: public Vehicle
{
//here you can add type specific code, that is code that is specific to Car
}
class Factory
{
std::vector<Vehicles> myVehicles;
}
So you're mapping a real life scenario to the domain of the program. Obviously there are many more examples, i have given just one of them.
The reason for doing this is that now you can handle different things about a particular factory and other real life objects. In this case a factory object has a std::vector of Vehicle's which represent the different vehicles that this particular Factory has. Simlarly you can have another factory lets say at Ohio which contain its own vehicle. So basically you are modelling real life problem through a program.
Generally it is all about using basic OOP principals in a scope of a program design. Programs are writing for human needs (domain scope). For example you need to print an essay, and you need a program for typing i.e. text editor. This text editor should have contain some user interface. This user interface can be logically split on some components like: text area, menu, menu times, status line, toolbar, buttons in tool bar, select file dialog for open and save operation etc.
To implement all of those components in your program, you can logically split it on sort of classes which were encapsulate data and code related to each component. As well as those classes expected to have some common behavior like show/hide, move etc. So you can implement it with a common class - widget. Then inherit all component classes from widget parent, so that you don't have to duplicate common code for: show, hide and move operations for all your components.
In menu you have an menu items, like open, save, exit etc. All items are similar so they can be implements by the same class in the same time, when you click on it operation should differ, i.e. open and save should open a select file dialog and then open or save file, when exit should save file and then close application. How to inject this operation inside a class without creating a sub-class for each menu item. You can create an fully virtual abstract class called Action with one fully virtual method, like perform. Then you can add a pointer on reference member into MenuIntem class. Then you can create a classes inherits Action, override fully virtual member - perform. Implementation of this perform will do some exact operation - like open file, safe file etc. And then you can inject reference on implementation into MenuItems objects. I.e. something like this:
struct Bounds {
std::size_t left, top, width, height;
}
class Widget {
Widget(const Widget&) = delete;
Widget& oprator=(const Widget&) = delete;
public:
Widget(Bounds& bounds) noexcept:
bounds_(bounds);
{}
virtual ~Widget() = default;
void move(std::size_t left, std::size_t top)
{
bounds_.left = left;
bounds_.top = top;
draw();
}
virtual void draw() {
// some platform specific implementation
}
virtual void show() {
// some platform specific implementation
}
virtual void hide() {
// some platform specific implementation
}
private:
bounds bounds_;
}
class Window: public Widget {
// some implementation
}
class SelectFileDialog: public Window {
// Some implemenation
}
....
class TextArea:public Widet
{
// some implementation
}
class StatusLine:public Widet
{
// some implementation
}
class Button: public Widget
{
public:
Button(Bounds& bounds, const std::string& caption):
Widget(),
caption_(caption)
{}
virtual void onClick() = 0;
private:
std::string caption_;
}
class Action {
Action(const Action&) = delete;
Action& operator=(Action&) = delete;
public:
virtual ~Action() = default;
virtual void perform() = 0;
}
class MenuItem: public Button {
public:
MenuItem(const std::string& cation, const std::shared_ptr<Action> action):
Button({0,0,100,30},cation),
action_(action)
{}
virtual void onClick() override
{
// real app should use some arguments
action_->perform();
}
private:
std::shared_ptr<Action> action_;
}
class SaveFileAction: public Action {
public:
void perform() override
{
SelectFileDialog dlg;
dlg.show();
// some save implementation
}
}
class OpenFileAction: public Action {
public:
void perform() override
{
SelectFileDialog dlg;
dlg.show();
// some open file implementation
}
}
class PrintFileAction: public Action {
public:
void perform() override
{
// some print file impementation
}
}
....
class MenuBox: public Widget
{
public:
MenuBox(Bounds& bounds):
Widget(bounds)
{}
void addItem(const std::string& caption, std::shared_ptr<Action> action)
{
items_.emplace_back( std::make_shared<MenuItem>( caption, action ) );
}
private:
std::vector< std::shared_ptr<MenuItem> > items_;
}
class MainWinow: public Window
{
public:
MainWidow():
Window({ 100, 100, 640, 480})
{}
void addComponent(const std::shared_ptr<Widget>& w)
{
childs_.epmplace_back(w);
}
virtual void show() override {
Window::show();
for(auto w: childs_) {
w->show();
}
}
pivate:
std::vector< std::shared_ptr<Widgets> > childs_;
}
int main(int argc, const char** argv) {
std::shared_ptr<MainWidow> w( new MainWindow() );
...
std::shared_ptr<MenuBox> mainMenu(new MenuBox() );
mainMenu->addItem("Open", std::shared_ptr<Action>(new OpenFileAction()) );
mainMenu->addItem("Save", std::shared_ptr<Action>(new SaveFileAction()) );
mainMenu->addItem("Print", std::shared_ptr<Action>(new PrintFileAction()) );
mainMenu->addItem("Exit", std::shared_ptr<Action>(new ExitAction()) );
...
w->addComponent(mainMenu);
w->addComponent(textArrea);
w->addComponent(statusLine);
...
w->show();
return 0;
}
Those this explains all basic OPP principles: encapsulation, aggregation, inheritance and polymorphism.
This is how OOP allow you to organize you application structure and help to reuse code. It is not for free i.e. all this abstractions cost memory and CPU time, for some small console programs OOP may not be needed. For most of complex programs benefits gives you a lot.
There are libraries of reusable classes you can use for different domains.
The most common is C++ standard library with io streams, time, threads etc. For other common scenarios you can look into boost
For graphical user interface there are a log Widget libraries like: wxWidgets, QT, GTK MM, Fltk and many others.
For video games there are special libraries and additional programs called engines - for example OGRE, Unreal Engine etc.
As well as there are libraries for implementing WEB and file severs, printing, image processing, AI and so one.
All this frameworks use object oriented programming concept to describe problem domain.

Function pointer, Functor or Lambda?

I'm relatively new to C++, having spent years with Obj-C, and wondering about how to add what would be closure block in Obj-C, to a C++ class. Here's some pseudo code of what I want to do:
class Slider
{
public:
void onMouseDown()
{
if(rightClick or ctlKeyDown)
{
if(myFunctionPointer != nil)
{
// show popup menu
myFunctionPointer(this);
}
}
}
FunctionPointer myFunctionPointer = nil;
};
class Editor
{
public:
void showPopupMenu(Slider *s)
{
// build the popupMenu with information based on the slider
}
void init()
{
// create a slider and connect the popupMenu function to it
mySlider = new Slider;
mySlider->functionPointer = showPopupMenu();
}
Slider *mySlider;
};
As you can see, I'm trying to get my Slider class to call a function without knowing anything about it.
This shouldn't be that difficult, but I'm interested in doing it the best/proper way. Lambdas and functors look incredibly confusing. Maybe I'm looking for something else. But what?
When it comes to treating function as objects, your basic options are: function pointer, functor/lambda, std::function. I am going to assume that you can find out their syntax and will focus on their difference.
A function pointer should be used when there is no need for a closure. This applies when the procedure you want to call is stateless or has a global state, and you have all parameters in the scope.
A functor should be used when you need to create a closure. Since functors are objects, you can maintain an internal state and pass parameters inside the closure.
A lambda is essentially a functor, without an explicit typename. The capture list of a lambda is its member if it were implemented as a functor instead. Note that you can overload operator() for a functor but not a lambda.
The problem with functor/lambdas is that each of their definition has a different type, and can be ill-suited in function signature/class member types. std::function resolves the problem by being able to accept functor/lambda/function pointer and convert them to a uniform type of std::function. You pay a (often small) price for this flexibility in the form of performance though.
Lambdas and functors are one of the most advanced C++ topics. You're better off starting with some fundamentals before, and have a solid understanding of how C++ classes work.
But, since you asked, the C++ equivalent of this should be something like this:
class Slider
{
public:
void onMouseDown()
{
if(rightClick or ctlKeyDown)
{
if(myFunctionPointer)
{
// show popup menu
myFunctionPointer(this);
}
}
}
std::function<void (Slider *)> myFunctionPointer=nullptr;
};
class Editor
{
public:
void showPopupMenu(Slider *s)
{
// build the popupMenu with information based on the slider
}
void init()
{
// create a slider and connect the popupMenu function to it
mySlider = new Slider;
mySlider->functionPointer = [this](Slider *)
{
showPopupMenu();
};
}
Slider *mySlider;
};
As I said, I think that you're better off focusing your efforts on getting the fundamentals down pat, first, before plunging into these shark-infested waters.
And just to add some additional color: this will compile (the only thing that's missing is the definitions of rightClick or ctlKeyDown), but it may or may not be right, depending on the scope and the lifetime of the objects involved. It may or may not be necessary to have the lambda capture a std::shared_ptr, instead of this, depending on how the objects in this application get instantiated. Understanding how C++ objects work would be a necessary prerequisite before dealing with closures and callbacks, of this sort.
There are different ways to achieve what you're looking for.
Here's a way, avoiding function pointers.
I didn't correct some other obvious mistakes, like the memory leak that arises from callingnew and never deleting the object.
Best practice in this case would be using a std::unique_ptr
class Slider
{
public:
Slider(Editor& e)
: _e(e)
{ }
void onMouseDown()
{
if(rightClick or ctlKeyDown)
{
_e.showPopupMenu(this);
}
}
Editor& _e;
};
class Editor
{
public:
void showPopupMenu(Slider *s)
{
// build the popupMenu with information based on the slider
}
void init()
{
// create a slider and connect the popupMenu function to it
mySlider = new Slider(*this);
}
Slider* mySlider;
};
Here's another solution moving a functor directly in the constructor, however using templates.
template <typename Handler>
class Slider
{
public:
Slider(Handler&& h)
: _h(std::move(h))
{ }
void onMouseDown()
{
if(rightClick or ctlKeyDown)
{
// show popup menu
_h(this);
}
}
Handler _h;
};
class Editor
{
public:
void showPopupMenu(Slider *s)
{
// build the popupMenu with information based on the slider
}
void init()
{
// create a slider and connect the popupMenu function to it
mySlider = new Slider([this](Slider* s){ showPopupMenu(s); });
}
Slider *mySlider;
};
You could also use a std::function instead, as shows on another answer

How to "skip" certain child class functions

I wasn't sure how to exactly title this, but I am trying to figure out something with polymorphism.
So basically, I want to have an array of the parent class (object) that holds a bunch of it's child classes (ones that are and aren't collidable). However, I want to be able to put this array into a loop and run the collision function for only the collidable child class, but since the other child class doesn't have a collide function, how can I do this?
(Looks something like this)
class Object
{
protected:
Image image; // Pseudo code to make point
public:
void Collision() = 0;
//Constructor/Destructor
Object(void);
~Object(void);
};
class Collidable : Object
{
private:
Position myPosition; // Pseudo code to make point
public:
void Collision(); // Has collision function for parent class
//Constructor/Destructor
Collidable(void);
~Collidable(void);
};
class Uncollidable : Object
{
private:
Position myPosition; // Pseudo code to make point
public:
// No collision function for parent class
//Constructor/Destructor
Uncollidable(void);
~Uncollidable(void);
};
int main()
{
Collidable collide1, collide2, collide3;
Uncollidable uncollide1, uncollide2, uncollide3;
Object *objects[] { collide1, collide2, uncollide1, uncollide2, uncollide3, collide3 };
for(int i = 0; i < 6; i++)
{
objects[i].Collide(); // Should not work.
}
return 0;
}
^(this was just an example to help show my question, do pardon some of the syntax errors if any)
I'm pretty sure, however, that something like this would be an error since void Collide() doesn't exist in the Uncollidable class. So how might I be able to still run the void Collide() function in the loop while avoiding error? Or is something like this impossible and I just have to make two separate arrays?
I hope I explained my question well.
(I tried to research this, but every time I tried I just got sent to the basics of polymorphism)
You can just do this:
for(int i = 0; i < 6; i++)
{
Collidable c = dynamic_cast<Collidable*>(objects[i]);
if(c != nullptr) // dynamic_cast will return null if objects[i] is not of type Collidable
c->Collide(); // Should work.
}
In your code there is one bug, you have made Collide() pure virtual in class Object, but you are not overriding it in Uncollidable. It will not work. Either override it in Uncollidable (which is inappropriate), or give a default body to Object::Collide() (which is inappropriate also).
There is a better design, put all the common interface in Object, separate out different behaviors in other interface. It will lead to good OO design ( compliant with IS-A relationship)
class Object
{
protected:
Image image; // Pseudo code to make point
public:
Object(void);
~Object(void);
//other common interface
};
class Collidable // this is an interface that represent 'collidable' behavior
{
public:
virtual void Collision() = 0;
}
class CollidableObject : public Object, public Collidable
{ ... }
class UncollidableObject : public Object
{ ... }
Note: Object must be inherited publicly, otherwise you will not be able to treat object os CollidableObject and UncollidableObject as object of Object.

c++ Setting a pointer variable in parent class from child and use it in parent class

i'm sorry for the title. I seem to have a problem. I'm just a beginner and i'm sorry if this was asked before.. i couldnt find a straight answer on this one. (when i search class, pointer and child i get results about passing parent or child pointers... i do not want to pass the (this) child or parent pointer, i just want to pass a pointer i initialized on a child class.. to the parent). What i'm trying to do here is better explained by code:
class App
{
public:
virtual void init(void) { window = &BasicWindow(); }
virtual void createWindow(void) { window->create(); }
protected:
Window *window;
};
class Game : public App
{
public:
virtual void init(void) { window = &OpenGLWindow(); }
};
int main ()
{
App *game = &Game();
game->init();
game->createWindow();
return 0;
}
Is this legal?
I have an abstract Window class from which BasicWindow and OpenGLWindow derives.
However, when i create the window i get an Access violation reading location error breaking at window->create() inside the App::createWindow() function.
Thanks
I'm guessing this is because you are pointing to a temporary:
window = &BasicWindow()
Once that function exits, window points to "crap" and bad things will happen.
presumably, what you want to do is to create a new instance of the window - i.e.
window = new BasicWindow();
Don't forget to cleanup!
I'm going to take a punt that you're coming from Objective-C? ;)
I think your problems all stem from not understanding how C++ objects are created.
First up: window = &BasicWindow(); is not how you should be creating a new object. You need to use window = new BasicWindow; This results in space for a BasicWindow being allocated in memory, and the default constructor for BasicWindow will be invoked.
Your have a similar error in your main() method, however in this case you do not need to use new to allocate it, you can just declare an instance and it will be created on the stack.
Your main method would then look like:
int main ()
{
Game game;
game.createWindow();
return 0;
}
The remaining problem is that your init methods are not being called. In C++ constructors are called automatically, and are named the same name as the class. An example default constructor for the game class would be:
Game() { window = new OpenGLWindow(); }
Another thing you need to know is that, unlike objective C, the entire hierarchy of constructors is called automatically when you create an object. That is, when you create an instance of Game, its constructor is called, as well as the constructor of every base class. In fact, the base class constructor is called FIRST. So in your case, if you just change the init methods to constructors, you'll allocate two windows (one of each type) and leak the BasicWindow. Which is not cool.
You should probably just leave them named init, and just make sure you call it immediately after creation.
In summary, try this:
class App
{
public:
virtual void init(void) { window = new BasicWindow; }
virtual void createWindow(void) { window->create(); }
protected:
Window *window;
};
class Game : public App
{
public:
virtual void init(void) { window = new OpenGLWindow; }
};
int main ()
{
Game game;
game.init();
game.createWindow();
return 0;
}
(and don't forget to cleanup the new'd objects!)
EDIT (added example complete with cleanup):
class App
{
public:
App() : window( NULL ) {}
virtual ~App() { delete window; }
virtual void init() { window = new BasicWindow; }
virtual void createWindow() { window->create(); }
protected:
Window *window;
};
class Game : public App
{
public:
virtual void init() { window = new OpenGLWindow; }
};
int main ()
{
Game game;
game.init();
game.createWindow();
return 0;
}
window is an uninitialized pointer of class App. Because, no where you are calling init method. So, window->create() results error, when base class createWindow() is called.
Edit 1:
As far as now, every thing is syntactically correct but amn't sure of what you are trying to achieve. Don't create temporary/nameless objects and assign them. Instead construct them with operator new in window = &BasicWindow(); and window = &OpenGLWindow();. Since the class manages resources, you should follow the principle Rule of Three. Also know that in statement -
App *game = new Game();
The static type of operand ( App* ) is different from the dynamic type( Game*). In such a case, the static type acts as a base class and it's destructor must be virtual or else the behaviour is undefined. So, the App class destructor must be virutal.
The error might be related to the fact that you are using pointers to temporaries.
virtual void init(void) { window = &BasicWindow(); }
This pointer becomes invalid after the ";". Use "new" instead of "&".
You need to call game->init() if you want to use the window pointer too (Even better put in in a constructor, thats what they are for).
Besides that, it is perfectly legal to change protected members of base classes.