Virtual Problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Christer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 44
Joined: Thu Feb 05, 2009 12:39 pm
Location: Sweden

Virtual Problem

Post 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
User avatar
dandymcgee
ES Beta Backer
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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Christer
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 44
Joined: Thu Feb 05, 2009 12:39 pm
Location: Sweden

Re: Virtual Problem

Post by Christer »

The functions is public. also class:
class Knight: public Piece{}...
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Virtual Problem

Post 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
User avatar
dandymcgee
ES Beta Backer
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

Post 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. :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: Virtual Problem

Post 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.
User avatar
TheBuzzSaw
Chaos Rift Junior
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

Post by TheBuzzSaw »

Are you calling move() on a dynamically allocated piece?
User avatar
dandymcgee
ES Beta Backer
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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply