473,408 Members | 2,839 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,408 software developers and data experts.

virtual methods, virtual bases bug<?> in G++ 3.3

Paragraph 10.3/2 in the C++ standard [ISO/IEC 14882:1998] has the
following code example:

<quote>
struct A {
virtual void f();
};
struct B : virtual A {
virtual void f();
};

struct C : B , virtual A {
using A::f;
};
void foo() {
C c;
c.f(); // calls B::f, the final overrider
c.C::f(); // calls A::f because of the usingdeclaration
}
</quote>

It seems a little odd (to me) that the method call 'c.f()' in function
foo() should invoke B::f. I thought it would invoke A::f due to the
using declaration 'using A::f;' in class C's declaration block. And in
fact, when I use G++ 3.3 to build the code sample shown below, the
method call 'c.f()' invokes A::f (which is what I expected), and not
B::f as stated in the C++ standard.

<example>

<code main.cpp>
#include <iostream>

struct A {
virtual void f() { std::cout << "A::f()\n"; }
};
struct B : virtual A {
virtual void f() { std::cout << "B::f()\n"; }
};
struct C : B, virtual A {
using A::f;
};

int main()
{
C c;
c.f(); // g++ 3.3 invokes A::f, not B::f
c.C::f();
}
</code main.cpp>

<build>
g++ main.cpp
</build>

<output>
A::f()
A::f()
</output>

</example>

FWIW, I looked around on the "JTC1/SC22/WG21 - C++" web site,

http://anubis.dkuug.dk/jtc1/sc22/wg21/

but I didn't find anything about this particular issue (e.g., in the
"Core Language Issues" pages). So I'd like some verification that c.f()
is indeed supposed to invoke B::f, as stated in the standard, and not A::f.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #1
3 1963
Jim Fischer wrote:
void foo() {
C c;
c.f(); // calls B::f, the final overrider
c.C::f(); // calls A::f because of the usingdeclaration
}
</quote>

It seems a little odd (to me) that the method call 'c.f()' in function
foo() should invoke B::f. I thought it would invoke A::f due to the
using declaration 'using A::f;' in class C's declaration block. And in
fact, when I use G++ 3.3 to build the code sample shown below, the
method call 'c.f()' invokes A::f (which is what I expected), and not
B::f as stated in the C++ standard.


I think there is some rational explanation.

If the member function name is not (scope) qualified (like c.f()) then
the final overrider is called.

If it is, the name used in the qualified scope is chosen. As shows the
example member function name does not have to be overridden, it may be
referred by using directive. But in this case such a name is not
considered when looking for overriders so the last overrider down in the
hierarchy is chosen.

Btw, Borland C++ compiles the example according to the standard.

Regards,
Janusz

Jul 19 '05 #2
"Jim Fischer" <jf***************@now.here.com> wrote...
Paragraph 10.3/2 in the C++ standard [ISO/IEC 14882:1998] has the
following code example:

<quote>
struct A {
virtual void f();
};
struct B : virtual A {
virtual void f();
};

struct C : B , virtual A {
using A::f;
};
void foo() {
C c;
c.f(); // calls B::f, the final overrider
c.C::f(); // calls A::f because of the usingdeclaration
}
</quote>

It seems a little odd (to me) that the method call 'c.f()' in function
foo() should invoke B::f. I thought it would invoke A::f due to the
using declaration 'using A::f;' in class C's declaration block. And in
fact, when I use G++ 3.3 to build the code sample shown below, the
method call 'c.f()' invokes A::f (which is what I expected), and not
B::f as stated in the C++ standard.


Did you actually read the paragraph preceding the example? Name
lookup for the call

c.f();

ignores names introduced by using declarations. That is, C::f is
ignored, and B::f is found.

So, G++ v3.3 is buggy. Name resolution is a tricky thing, not all
of the implementations do it right.

Victor
Jul 19 '05 #3
Janusz Szpilewski wrote:
Jim Fischer wrote:
void foo() {
C c;
c.f(); // calls B::f, the final overrider
c.C::f(); // calls A::f because of the usingdeclaration
}
</quote>

It seems a little odd (to me) that the method call 'c.f()' in function
foo() should invoke B::f. I thought it would invoke A::f due to the
using declaration 'using A::f;' in class C's declaration block. And in
fact, when I use G++ 3.3 to build the code sample shown below, the
method call 'c.f()' invokes A::f (which is what I expected), and not
B::f as stated in the C++ standard.


I think there is some rational explanation.

If the member function name is not (scope) qualified (like c.f()) then
the final overrider is called.

If it is, the name used in the qualified scope is chosen. As shows the
example member function name does not have to be overridden, it may be
referred by using directive. But in this case such a name is not
considered when looking for overriders so the last overrider down in the
hierarchy is chosen.

Btw, Borland C++ compiles the example according to the standard.


Thanks for the reply. I understand the meaning of paragraph 10.3/2
(although the description given in the standard is different from what I
originally expected, but of course that's irrelevant). I was basically
looking for confirmation that 10.3/2 still applies -- that there have
not been any official changes to that particular paragraph -- before
submitting a bug report to the GNU g++ maintainers. FWIW, this afternoon
I submitted a G++ bugzilla report on this issue.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #4

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

Similar topics

1
by: Chua Wen Ching | last post by:
Okay i manage to convert the C# class library into dll, and enable com interop. I basically can call c# methods from vb6 with this as long i add references to the .net project (e.g TestProject) ...
0
by: Hareth | last post by:
In VS2003: I open Form1.cs w/ the "open with" menu, then open the file w/ notepad, it works fine but after installing VS2005, "open with" doesnt function correctly. If I try to open with...
1
by: Tomas | last post by:
Is there any sequence diagram on the web that clearly shows in which order all Page methods (load, render and so on) are being called compared to the order the page's contained control methods are...
34
by: antonyliu2002 | last post by:
I've set up the virtual smtp server on my IIS 5.1 like so: 1. Assign IP address to "All Unassigned", and listen to port 25. 2. Access Connection granted to "127.0.0.1". 3. Relay only allow...
8
by: gerry | last post by:
The PagerSettings.Visible property is not being handled properly by the GridView control. The value assigned to this property is not applied until after a postback. Simplest test : .aspx...
3
by: int19h | last post by:
While developing my own serialization library, I've found a need to detect, for a given pair of classes B and D, such that D directly derives from B, whether B is a virtual or non-virtual base of...
4
by: python | last post by:
Is there an official list of all Python's __<special-methods>__? I'm learning Python and trying to get an idea of what __<special-methods>__ are available and specifically, what...
6
by: Rob McDonald | last post by:
I would like to force all the classes in my hierarchy to implement the << operator for testing purposes. My base class is a pure virtual class. I started out by working with operator...
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: 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
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.