I have a problem with a virutal function.
Board.h
#include "Knight.h"
class Board
{
//Board hava a array with Pieces
};
Piece.h
class Piece
{
virtual void move()
{
blablabla
};
};
Knight.h
#include "Piece.h"
Knight: public Piece
{
void move()
{
blablabla
};
}:
...
So when I try this inside a function or whatever in board:
Pieces[0].move();
it will use the Piece move() not the Knight's move(). Anyone understand why? :S
Virtual Problem
Moderator: Coders of Rage
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Virtual Problem
I find it extraordinarily difficult to read that code. Please try using code tags and indenting if you want anyone to keep this topic open for more than 10 seconds.
Edit: I did it for you 'cause I'm feeling nice:
Board.h
Piece.h
Knight.h
And to answer your question, classes are private by default and you can't inherit private function (even if their virtual I believe..?).
Try the above code and let me know what happens.
Edit: I did it for you 'cause I'm feeling nice:
Board.h
Code: Select all
#include "Knight.h"
class Board
{
//Board hava a array with Pieces
};
Code: Select all
class Piece
{
public: //I've added the word public here so that Knight inherits this function
virtual void move()
{
blablabla
};
};
Code: Select all
#include "Piece.h"
Knight: public Piece
{
void move()
{
blablabla
};
}:
Try the above code and let me know what happens.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: Virtual Problem
The functions is public. also class:
class Knight: public Piece{}...
class Knight: public Piece{}...
Re: Virtual Problem
If you don't specify "public:", which dandymcgee wrote in his modified code, everything is private by default.Christer wrote:The functions is public. also class:
class Knight: public Piece{}...
You can use "private Piece" and still get desired results, but then if you were to create a child class from Knight, only public/protected-declared items would get inherited, since all of Piece's are private.
If that makes sense :D
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Virtual Problem
No he cannot. That would inherit Piece's protected and public members (there aren't any) as private members in Knight. You cannot inherit Piece's private members.Scoody wrote: You can use "private Piece" and still get desired results...
If that makes sense :D
That tells Knight to inherit all public and protected member variables and functions from the Piece class. Private members (Piece's Move() function is private) CANNOT be inherited. You must specify either public or protected in the Piece class BEFORE declaring the Move() function, or the Knight class will not inherit it.Christer wrote:The functions is public. also class:
class Knight: public Piece{}...
I hope I explained this well enough for you to understand.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: Virtual Problem
That's what I meant, though I seem to have formulated myself very badly. I was thinking along the lines that he thought since he "public'ed" Knight, it should've worked.dandymcgee wrote:No he cannot. That would inherit Piece's protected and public members (there aren't any) as private members in Knight. You cannot inherit Piece's private members.Scoody wrote: You can use "private Piece" and still get desired results...
If that makes sense :D
- TheBuzzSaw
- Chaos Rift Junior
- Posts: 310
- Joined: Wed Dec 02, 2009 3:55 pm
- Current Project: Paroxysm
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Contact:
Re: Virtual Problem
Are you calling move() on a dynamically allocated piece?
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Virtual Problem
Yeah I knew what you meant, but I wasn't sure if he would. Speaking of which.. where'd this guy go?Scoody wrote:That's what I meant, though I seem to have formulated myself very badly. I was thinking along the lines that he thought since he "public'ed" Knight, it should've worked.dandymcgee wrote:No he cannot. That would inherit Piece's protected and public members (there aren't any) as private members in Knight. You cannot inherit Piece's private members.Scoody wrote: You can use "private Piece" and still get desired results...
If that makes sense :D
Why would this make any difference?... just curious.TheBuzzSaw wrote:Are you calling move() on a dynamically allocated piece?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!