By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

virtual from Base-of-Base?

P: n/a
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?
Nov 14 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
..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
Nov 14 '08 #2

P: n/a
On 14 Nov., 19:11, Victor Bazarov <v.Abaza...@comAcast.netwrote:
.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() ?
Nov 14 '08 #3

P: n/a
On Nov 14, 1:22*pm, ".rhavin grobert" <cl...@yahoo.dewrote:
On 14 Nov., 19:11, Victor Bazarov <v.Abaza...@comAcast.netwrote:
.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.

Nov 14 '08 #4

P: n/a
..rhavin grobert wrote:
On 14 Nov., 19:11, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>.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
Nov 14 '08 #5

P: n/a
..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
Nov 15 '08 #6

P: n/a
On Nov 14, 11:00 pm, ".rhavin grobert" <cl...@yahoo.dewrote:
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
Nov 17 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.