473,378 Members | 1,420 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.

Legal to override same function in different bases?

Is the following code well-defined, and outputs foo twice?

#include <iostream>

struct Base1 { virtual void foo() = 0; };
struct Base2 { virtual void foo() = 0; };

struct Derived: Base1, Base2
{
void foo() { std::cout << "foo" << std::endl; }
};

int main()
{
Derived d;
Base1 *b1 = &d;
Base2 *b2 = &d;
b1->foo(); b2->foo();
}

Oct 7 '07 #1
2 1238
On Oct 8, 9:52 am, Old Wolf <oldw...@inspire.net.nzwrote:
Is the following code well-defined, and outputs foo twice?

#include <iostream>

struct Base1 { virtual void foo() = 0; };
struct Base2 { virtual void foo() = 0; };

struct Derived: Base1, Base2
{
void foo() { std::cout << "foo" << std::endl; }
};

int main()
{
Derived d;
Base1 *b1 = &d;
Base2 *b2 = &d;
b1->foo(); b2->foo();
}
All compilers I have used will do exactly as you describe (output "foo
\nfoo\n"). It makes little sense to do otherwise doesn't it ?
Sometimes, however, I would like to prevent any override.

Oct 8 '07 #2
On Oct 8, 1:52 am, Old Wolf <oldw...@inspire.net.nzwrote:
Is the following code well-defined, and outputs foo twice?
#include <iostream>
struct Base1 { virtual void foo() = 0; };
struct Base2 { virtual void foo() = 0; };
struct Derived: Base1, Base2
{
void foo() { std::cout << "foo" << std::endl; }
};
int main()
{
Derived d;
Base1 *b1 = &d;
Base2 *b2 = &d;
b1->foo(); b2->foo();
}
Yes. It's fully defined. IIRC, this is even discussed some in
Stroustrup (one of the editions, anyway). The issue, of course,
is if Base1 is actuall GraphicObject, Base2 CardDeck, and the
function "draw()".

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
5
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code;...
14
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to...
1
by: Steve | last post by:
I would like to override the IsInputKey function in order to trap the arrow up/down keys in the Key Events. I understand that arrow keys don't have a char representation and thus, hard to trap. ...
9
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict,...
6
by: Dave Vandervies | last post by:
I'm writing an abstraction layer over an OS's ability to do non-blocking reads and writes on various things. The basic idea is that the caller will be able to let the I/O happen in the background,...
8
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have...
2
by: dolphin | last post by:
Hi everyone . I am confused about the different between override overload and hide. May be I have a wrong opinion, I always think that hide is very similar with override. Both of them call the...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.