[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

virtual from Base-of-Base?

.rhavin grobert

11/14/2008 6:01:00 PM

guess you have ....

class Grandbase {
public:
virtual int foo();
};

class Base: public Grandbase {
/* no foo() specified here */
};

class Derived, public Base {
public:
virtual int foo();
};

my compiler gives no errors, ..but ... is it safe?
8 Answers

Richard Eich

6/15/2008 6:13:00 PM

0

"Enviromentally Whacko" <whacko @Democrap.org> wrote...
>
> "boo-radley" <mpmull2u@gmail.com> wrote in message
> > On Jun 14, 4:25 pm, "Enviromentally Whacko" <whacko @Democrap.org>
> > wrote:
> >> http://www.breitbart.tv/html/1...
> >
> > Get your McSame dolls before they fall apart.
> =============
> What is McSame? Is that something that you got free in your Happy Meal
> today?

Close enough.

--
"Three things that politicians hide behind: The flag, the Bible, and
children." George Carlin, "Life is Worth Losing"

GOP Ruined America

6/16/2008 4:54:00 AM

0


"Enviromentally Whacko" <whacko @Democrap.org> wrote in message
news:VNWdnXZmleQDqcjVnZ2dnUVZ_qHinZ2d@comcast.com...
>
> "boo-radley" <mpmull2u@gmail.com> wrote in message
> news:e4603b5d-d31b-4602-b24a-eca8765dc717@t54g2000hsg.googlegroups.com...
>> On Jun 14, 4:25 pm, "Enviromentally Whacko" <whacko @Democrap.org>
>> wrote:
>>> http://www.breitbart.tv/html/1...
>>
>> Get your McSame dolls before they fall apart.
> =============
> What is McSame? Is that something that you got free in your Happy Meal
> today?

The McSame McKook dolls come in the color, ghost white with a yellow belly.
When you pull the string McSame says, "I surrender, Uncle Ho".
Then, "Get on your knees and suck it, Larry".
Finally, "100 years in Iraq doesn't bother me". Then it shits it's
flightsuit.


Victor Bazarov

11/14/2008 6:11:00 PM

0

..rhavin grobert wrote:
> guess you have ....
>
> class Grandbase {
> public:
> virtual int foo();
> };
>
> class Base: public Grandbase {
> /* no foo() specified here */
> };
>
> class Derived, public Base {
> public:
> virtual int foo();
> };
>
> my compiler gives no errors, ..but ... is it safe?

Define "safe".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

.rhavin grobert

11/14/2008 6:23:00 PM

0

On 14 Nov., 19:11, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> .rhavin grobert wrote:
> > guess you have ....
>
> > class Grandbase {
> > public:
> >    virtual int foo();
> > };
>
> > class Base: public Grandbase {
> >    /* no foo() specified here */
> > };
>
> > class Derived, public Base {
> > public:
> >    virtual int foo();
> > };
>
> > my compiler gives no errors, ..but ... is it safe?
>
> Define "safe".
>

does a...

void callfoo(Grandbase* b)
{
b->foo();
};

Derived d;
callfoo(d);

always call Derived::foo() ?

Salt_Peter

11/14/2008 7:32:00 PM

0

On Nov 14, 1:22 pm, ".rhavin grobert" <cl...@yahoo.de> wrote:
> On 14 Nov., 19:11, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
>
>
> > .rhavin grobert wrote:
> > > guess you have ....
>
> > > class Grandbase {
> > > public:
> > >    virtual int foo();
> > > };
>
> > > class Base: public Grandbase {
> > >    /* no foo() specified here */
> > > };
>
> > > class Derived, public Base {
> > > public:
> > >    virtual int foo();
> > > };
>
> > > my compiler gives no errors, ..but ... is it safe?
>
> > Define "safe".
>
> does a...
>
> void callfoo(Grandbase* b)
> {
>   b->foo();
>
> };
>
> Derived d;
> callfoo(d);
>
> always call Derived::foo() ?

yes, and callfoo() can also be a member function of Grandbase
It'll call the appropriate virtual foo()
with respect of the type of the object calling it.
Thats still the case even if foo() is private.

#include <iostream>

class Grandbase
{
virtual int foo()
{
std::cout << "GrandBase::foo()\n";
return 0;
}
public:
int callfoo() { return foo(); }

};

class Base : public Grandbase {

};

class Derived : public Base
{
int foo()
{
std::cout << "Derived::foo()\n";
return 0;
}
};

int main()
{
Base b;
Derived d;

b.callfoo();
d.callfoo();
}

/*
GrandBase::foo()
Derived::foo()
*/

I see you passing pointers around, prefer references to constant.
Why?
If you allocate and deallocate using Grandbase* pointers, you'll get
memory leaks because your Grandbase class doesn't have a virtual
d~tor.
What you can't do is declare foo() in Grandbase pure virtual and then
attempt to create an instance of Grandbase or Base.

Victor Bazarov

11/14/2008 8:47:00 PM

0

..rhavin grobert wrote:
> On 14 Nov., 19:11, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> .rhavin grobert wrote:
>>> guess you have ....
>>> class Grandbase {
>>> public:
>>> virtual int foo();
>>> };
>>> class Base: public Grandbase {
>>> /* no foo() specified here */
>>> };
>>> class Derived, public Base {
>>> public:
>>> virtual int foo();
>>> };
>>> my compiler gives no errors, ..but ... is it safe?
>> Define "safe".
>>
>
> does a...
>
> void callfoo(Grandbase* b)
> {
> b->foo();
> };
>
> Derived d;
> callfoo(d);
>
> always call Derived::foo() ?

It won't compile. You probably meant

Derived d;
callfoo(&d);

(and BTW no semicolon after the body of 'callfoo' either), in which
case, yes, it does. And why do you ask here, why don't you just try it?
And what book are you reading that doesn't explain the concept of the
"final overrider", AFA virtual functions are concerned?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Marcel Müller

11/15/2008 9:52:00 AM

0

..rhavin grobert schrieb:
> guess you have ....
>
> class Grandbase {
> public:
> virtual int foo();
> };
>
> class Base: public Grandbase {
> /* no foo() specified here */
> };
>
> class Derived, public Base {
> public:
> virtual int foo();
> };
>
> my compiler gives no errors, ..but ... is it safe?

Same here, but if you remove the syntax error after class Derived, you
may have more luck.


Marcel

mail.dsp

11/17/2008 7:33:00 AM

0

On Nov 14, 11:00 pm, ".rhavin grobert" <cl...@yahoo.de> wrote:
> guess you have ....
>
> class Grandbase {
> public:
> virtual int foo();
>
> };
>
> class Base: public Grandbase {
> /* no foo() specified here */
>
> };
>
> class Derived, public Base {
> public:
> virtual int foo();
>
> };
>
> my compiler gives no errors, ..but ... is it safe?

Definitely it will not give any error because Base class has also int
foo() method derived from Grandbase and it will remain virtual inside
class Base. All the derived class of Base can override int foo(). To
clear these concepts study The C++ Programming Language by Stroustrup.

--
Daya