Page 1 of 1

Virtual Problem

Posted: Thu Dec 10, 2009 10:31 am
by Christer
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

Re: Virtual Problem

Posted: Thu Dec 10, 2009 11:11 am
by dandymcgee
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

Code: Select all

#include "Knight.h"
class Board
{
    //Board hava a array with Pieces
};
Piece.h

Code: Select all

class Piece
{
    public: //I've added the word public here so that Knight inherits this function
        virtual void move()
        {
            blablabla
        };
};
Knight.h

Code: Select all

#include "Piece.h"
Knight: public Piece
{
    void move()
    {
        blablabla
    };
}:
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.

Re: Virtual Problem

Posted: Thu Dec 10, 2009 11:37 am
by Christer
The functions is public. also class:
class Knight: public Piece{}...

Re: Virtual Problem

Posted: Thu Dec 10, 2009 1:06 pm
by Scoody
Christer wrote:The functions is public. also class:
class Knight: public Piece{}...
If you don't specify "public:", which dandymcgee wrote in his modified code, everything is private by default.
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

Re: Virtual Problem

Posted: Fri Dec 11, 2009 10:32 am
by dandymcgee
Scoody wrote: You can use "private Piece" and still get desired results...
If that makes sense :D
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.
Christer wrote:The functions is public. also class:
class Knight: public Piece{}...
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.

I hope I explained this well enough for you to understand. :)

Re: Virtual Problem

Posted: Sun Dec 13, 2009 11:57 am
by Scoody
dandymcgee wrote:
Scoody wrote: You can use "private Piece" and still get desired results...
If that makes sense :D
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.
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.

Re: Virtual Problem

Posted: Sun Dec 13, 2009 2:10 pm
by TheBuzzSaw
Are you calling move() on a dynamically allocated piece?

Re: Virtual Problem

Posted: Sun Dec 13, 2009 4:21 pm
by dandymcgee
Scoody wrote:
dandymcgee wrote:
Scoody wrote: You can use "private Piece" and still get desired results...
If that makes sense :D
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.
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.
Yeah I knew what you meant, but I wasn't sure if he would. Speaking of which.. where'd this guy go?
TheBuzzSaw wrote:Are you calling move() on a dynamically allocated piece?
Why would this make any difference?... just curious.