Boost logo

Boost Users :

Subject: Re: [Boost-users] [Memory] Problems when calling virtual class function
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2010-07-11 15:28:35


On Jul 11, 2010, at 12:17 PM, Janez Urevc wrote:

> Hello,
>
> I am doing a research about Monte Carlo Tree Search efficiency on KBNK chess
> ending. I use framework and algorithm implementation in C++. Since I had some
> memory leaks, that I couldn't manage to identify, I decided to try
> Boost's shared_ptr.
> I reorganized the whole thing to use it, but I encountered a problem,
> which I cannot
> solve.
>
> The problem occurs, when I try to call class member function, which
> was declared
> "virtual" in super class. If I try to call functions, that are not
> redefined, everything
> works as expected. Ex.:
>
> class Base{
> public:
> int val = 0;
> virtual void fun(){ /* do something */}
> };
>
> class Derived : public Base{
> public:
> void fun(){ /*redefined*/}
> void fun1(){ /*not redefined*/}
> };

If you declare "fun" as virtual in the base class, it needs to be virtual in the derived class, too.
Many compilers will warn you about this.

-- Marshall


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net