Home » Coding » c++ » Legal to override same function in different bases?
Legal to override same function in different bases? [message #17414] Sun, 07 October 2007 19:52 Go to next message
Old Wolf  is currently offline Old Wolf
Messages: 44
Registered: August 2007
Member
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();
}
Re: Legal to override same function in different bases? [message #17415 is a reply to message #17414 ] Mon, 08 October 2007 00:29 Go to previous messageGo to next message
gi4nospam  is currently offline gi4nospam
Messages: 1
Registered: October 2007
Junior Member
On Oct 8, 9:52 am, Old Wolf <oldw...@inspire.net.nz> wrote:
> 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.
Re: Legal to override same function in different bases? [message #17417 is a reply to message #17414 ] Mon, 08 October 2007 00:52 Go to previous message
alfps  is currently offline alfps
Messages: 315
Registered: July 2007
Senior Member
* Old Wolf:
> 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();
> }

Except for the issue of also including <ostream> (AFAIUI now resolved in
favor of not needed), yes.

Just to be sure I didn't overlook any speling mstake or such I ran the
code through Comeau Online and it compiled fine.

Given that there are no trivial mistakes in spelling and so on, the
effect of overriding in multiple bases is well-defined. However, that
makes it problematic to not override. In order to override some but not
all foo's it's necessary to introduce intermediate classes.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Previous Topic:Release file descriptor in c/c++
Next Topic:which language allows you to change an argument's value?
Goto Forum:
  


Current Time: Fri May 16 02:34:10 EDT 2008

Total time taken to generate the page: 0.89597 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software