473,378 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

virtual from Base-of-Base?

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
6 1483
..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
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
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
..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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wendy Elizabeth | last post by:
I am installing the microsoft virtual pc 2004 and the service pack 1 for the first time and I am getting the following message: Virtual PC could not open the Virtual Machine Network Services...
7
by: Alex Vinokur | last post by:
Hello, Here is some program with virtual constructors. Is there any difference between * clone1() vs. clone2() * create1() vs. create2() ? It seems that it should be.
4
by: aap | last post by:
Hi, I have the following code. #include <iostream> using namespace std; class Root { public: virtual void Root1() = 0; virtual void Root2() = 0;
10
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public:...
8
by: RafaƂ Maj Raf256 | last post by:
#include <iostream> #include <string> #include <vector> using namespace std; // --- from librarry, can't edits thoes 4 lines below --- class in1 { public: }; class in2 : public in1 {...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
6
by: zissop | last post by:
Hello I have a strange situation with my ASP.Net applications running on Windows 2003 Server x64. As soon as they get instantiated (a visitor hits a page). the virtual memory they allocate goes to...
6
by: George2 | last post by:
Hello everyone, Here is my more simplified case to show virtual function is missing in vtable. In class Goo, function myFunc is missing in vtable, but in class Zoo, it is ok to contain virtual...
1
by: =?Utf-8?B?QiBTaW5naA==?= | last post by:
Hi Scenario 1) Host - XP Laptop with internet connection using Sky Broadband Wireless Router Netgear DG834GT 2) MS Virtual Server 2005 - I have deployed an XP MS Virtual Server 2005 to act...
5
by: rockdale | last post by:
Hi, all: Hope somebody can solve this problem for me. We are migrating 3 asp website into IIS 6.0 on a server 2003. we do not want to create 3 website since we do not want to acquire different...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.