• Advertisement
Sign in to follow this  

Question about c++ Inheritance

This topic is 4147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I thought I understood inheritance and virtual methods, but I just ran into a problem, and honestly, i dont use it all that much. OK, so I know that this works
class a
{
     virtual void DoThat(){}
}; 

class b : public a // b inherits from a
{
     void DoThat(){printf("Done!");} // and overrides the method
};

//and now somewhere in my code i can have this

void takesAasParam(a *param)
{
    param->DoThat();
}

//and pass it a 'b' instead of an a
takesAasParam(&instanceOfB);

now thats nifty, but I thought inheritance could do this too

class a
{
     virtual void DoThis(){printf("class a");}
     void DoOther(){DoThis();} //this function calls the virtual method
};

class b : public a
{
     void DoThis(){printf("class b");}
};

//now, i thought that calling this
b instanceOfB;
b.DoOther(); 
// would print "class b", but it calls a's method instead of b's.

I guess that makes sense, since it is a's method that is doing the calling, not b's. So i guess thats just my misconception. Since that wont work, how can I implement the above idea?

Share this post


Link to post
Share on other sites
Advertisement
If the code you're using looks like that, then it should work. My test printed "class b":


class A
{
public:
virtual void DoThis() { std::cout<<"class a"<<std::endl; }
void DoOther() { DoThis(); }
};

class B : public A
{
public:
void DoThis() { std::cout<<"class b"<<std::endl; }
};

int main()
{
B MyB;
MyB.DoOther();
return 0;
}



Are you sure that you've declared your function as virtual?

I know it's not always possible, but if you could post some actual code it would be easier to help.

HTH!

Share this post


Link to post
Share on other sites
Any method called "virtual" is like a function pointer.

If you want DoOther to call A::DoThis, you can do the following:

class a
{
void DoThisA(){printf("class a");}
virtual void DoThis(){DoThisA()}
void DoOther(){DoThisA();}
};

class b : public a
{
void DoThis(){printf("class b");}
};

//now, i thought that calling this
b instanceOfB;
b.DoOther();



Share this post


Link to post
Share on other sites

ok heres some code straight from the IDE. I write pretty horrible code, so try to control yourselves and focus on the problem at hand, and not my other coding sins(there is at least one infinite loop in there). plus i really just kind of through this together to try something out, i wasnt aiming for a class i can reuse throughout other projects(though i guess that would be a good idea).

SDLWindow.h

// SDLWindow.h
#pragma once

#include <sdl/sdl.h>
#include <windows.h>
#include <gl/gl.h>


class SDLWindow
{
protected:


SDL_Surface *m_surIscreen;

Uint8* m_uinIkeyState;
Uint8* m_uinIprevKeyState;

int m_intImouseX, m_intImouseY;
Uint8 m_uinImouseState;

void Run();

void UpdateBackground();

bool Init();

bool m_booIdone;

protected:

static const int RIGHTCLICK = 1;
static const int LEFTCLICK = 3;

bool KeyDown(int a_intIkey);
bool KeyPress(int a_intIkey);

bool Done();
void Kill();

virtual bool VInit();
virtual void VUpdate();
virtual void VDraw();
public:
SDLWindow(void);
~SDLWindow(void);
};



SDLWindow.cpp

//SDLWindow.cpp
#include "SDLWindow.h"

SDLWindow::SDLWindow(void)
{
m_booIdone = false;

SDL_Init(SDL_INIT_VIDEO);

m_surIscreen = SDL_SetVideoMode(640,480,16,SDL_OPENGL);
printf("entering init...\n");

if(!Init())
return;
printf("entering main loop...\n\n");
Run();
}

SDLWindow::~SDLWindow(void)
{
SDL_Quit();
}

bool SDLWindow::VInit()
{
return true;
}

bool SDLWindow::Init()
{
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
glClearColor(0.0f,0.0f,0.0f,0.0f);

return VInit();
}

void SDLWindow::UpdateBackground()
{
SDL_PumpEvents();
m_uinIprevKeyState = m_uinIkeyState;
m_uinIkeyState = SDL_GetKeyState(NULL);

m_uinImouseState = SDL_GetMouseState(&m_intImouseX, &m_intImouseY);
}

void SDLWindow::Run()
{
while(true)
{
UpdateBackground();

VUpdate();
VDraw();
}
}

bool SDLWindow::KeyDown(int key)
{
if(m_uinIkeyState[key])
return true;
return false;
}

void SDLWindow::Kill()
{
m_booIdone = true;
}
bool SDLWindow::Done()
{
return m_booIdone;
}
void SDLWindow::VDraw()
{
printf("SDLWindow::VDraw()\n");
}
void SDLWindow::VUpdate()
{
}




SimWindow.h

//SimWindow.h
#pragma once

#include "SDLWindow.h"

class SimWindow : private SDLWindow
{
public:
SimWindow(void);
~SimWindow(void);

virtual bool VInit();
virtual void VUpdate();
virtual void VDraw();
};




SimWindow.cpp

//SimWindow.cpp
#include "SimWindow.h"

SimWindow::SimWindow(void)
{

}

SimWindow::~SimWindow(void)
{
}

bool SimWindow::VInit()
{
glOrtho(0,640,0,480,-1,1);
return true;
}

void SimWindow::VUpdate()
{

}

void SimWindow::VDraw()
{
glClear(GL_COLOR_BUFFER_BIT);

SDL_GL_SwapBuffers();
printf("SimWindow::VDraw()\n");
}



Basically, the idea is, SDLWindow handles window creation etc, and enters a main loop. Theres are three overrideable methods, VInit, VUpdate, and VDraw. VInit should be called to do any extra initialization the app needs on top of SDLWindow, VUpdate would be for updating anything, and VDraw would draw the screen.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement