Hello

class A

{

public:

virtual void Func1() = 0;

void Func1Caller()

{

Func1();

}

~A()

{

Func1Caller();

}

};

class B : public A

{

public:

virtual void Func1()

{

printf("this is func1\n");

}

};

int main()

{

B t;

return 0;

}



Can someone tell me how to solve my problem? It seems that VC is not
allowing Func1Caller() to call Func1() from the destructor.

Can someone clarify?

--

Elias

Re: clarification - calling virtual function from destructor by Dave

Dave
Fri Feb 06 04:49:50 CST 2004

Two things. you should make the destructor of the base class virtual.
Func1() with it. So it dose not exist when the destructor of the base class
is called.

I have not tried this but I believe the derived class has already been
destroyed taken the overridden Func1() with it. So it dose not exist when
the destructor of the base class is called.

Dave


"lallous" <lallous@lgwm.org> wrote in message
news:OAdj1mJ7DHA.2416@TK2MSFTNGP10.phx.gbl...
> Hello
>
> class A
>
> {
>
> public:
>
> virtual void Func1() = 0;
>
> void Func1Caller()
>
> {
>
> Func1();
>
> }
>
> ~A()
>
> {
>
> Func1Caller();
>
> }
>
> };
>
> class B : public A
>
> {
>
> public:
>
> virtual void Func1()
>
> {
>
> printf("this is func1\n");
>
> }
>
> };
>
> int main()
>
> {
>
> B t;
>
> return 0;
>
> }
>
>
>
> Can someone tell me how to solve my problem? It seems that VC is not
> allowing Func1Caller() to call Func1() from the destructor.
>
> Can someone clarify?
>
> --
>
> Elias
>
>



Re: clarification - calling virtual function from destructor by lallous

lallous
Fri Feb 06 05:15:48 CST 2004

"Dave" <DAVE@XYZ.COM> wrote in message
news:#6nRz7J7DHA.2540@TK2MSFTNGP11.phx.gbl...
> Two things. you should make the destructor of the base class virtual.
> Func1() with it. So it dose not exist when the destructor of the base
class
> is called.
>
I tried but still there is a runtime error!

> I have not tried this but I believe the derived class has already been
> destroyed taken the overridden Func1() with it. So it dose not exist when
> the destructor of the base class is called.
hmm...that is what I was missing.
I thought that the derived class will be destroyed when the base class is.

Do you know how I can solve my problem/design?

"class A" knows everything but allocation/freeing, and "class B" only
provides allocate/free methods

class A
{
public:
virtual void Allocate() = 0;
virtual void Free() = 0;

void FreeAll()
{
Free();
}

virtual ~A()
{
FreeAll();
}
};


class B : public A
{
public:
virtual void Free()
{
printf("FreeAll()\n");
}

virtual void Allocate()
{
printf("Allocate()\n");
}
};


int main()
{
B t;

return 0;
}

--
Elias



Re: clarification - calling virtual function from destructor by Hendrik

Hendrik
Fri Feb 06 08:43:23 CST 2004

lallous <lallous@lgwm.org> wrote:
> [...]
> > I have not tried this but I believe the derived class has already been
> > destroyed taken the overridden Func1() with it. So it dose not exist when
> > the destructor of the base class is called.
> hmm...that is what I was missing.
> I thought that the derived class will be destroyed when the base class is.

No. Then it would be living while the
base class part is already dead. Surely
not very safe -- not to say that it is
not very nice either... :)
When invoked from a ctor/dtor, virtual
functions of derived classes will never
be called. That is because, in the ctor,
there is no object of the derived class
yet, and in the dtor, it isn't anymore.

> Do you know how I can solve my problem/design?

What you are asking for is a post-ctor
function. That doesn't exist in C++. As
often, this can be solved by adding a
level of indirection. Instead of this:

class X {
X() {f();}
virtual ~X() {}
virtual void f() {}
virtual void g() {}
};

You have this:

class X_outer {
public:

X_outer() : pi(createInner_()) {pi->f();}

~X_outer() {delete pi;}

void g() {pi->g();}

private:
// forbidden (not implemented)
X_outer(const X_outer&);
X_outer& operator=(const X_outer&);

class X_inner {
X_inner();
virtual ~X_inner();
virtual void f();
virtual void g();
};

X_inner* pi;

X_inner* createInner_();
};

Now, 'X_outer::createInner_()' can create
objects of classes derived from 'X_inner'.
The ctor of 'X_outer' will have it create
the object and call 'f()' only when the
object is fully created.

> [...]


Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers