473,322 Members | 1,232 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,322 software developers and data experts.

Calling identically-named parent method

Hello,

Consider the following code.

$ cat foo.C
#include <cstdio>

struct A
{
int bar() { puts("A::bar()"); return 666; }
int Abar() { puts("A::bar()"); return 666; }
};

struct B : public A
{
void bar(int n) { printf("B::bar(%d)\n", n); }
};

int main()
{
B foo;
foo.bar(123);
foo.bar();
foo.A::bar();
foo.Abar();
return 0;
}

Both A and B provide a method 'bar' but the signature is different.
int bar(void) in A
void var(int) in B

I have an object of type B, and I want to call A's bar.

The ovious foo.bar() is considered a syntax error by GCC.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.C
foo.C: In function `int main()':
foo.C:18: error: no matching function for call to `B::bar()'
foo.C:11: note: candidates are: void B::bar(int)

Does this mean I either have to change the name of A's bar as in Abar,
or qualify bar as in A::bar? Are there other alternatives?

Regards.
Dec 20 '07 #1
2 1624
On Dec 20, 1:42 pm, Spoon <root@localhostwrote:
Does this mean I either have to change the name of A's bar as in Abar,
or qualify bar as in A::bar? Are there other alternatives?
struct B : public A
{
using A::bar;
void bar(int n) { printf("B::bar(%d)\n", n); }

};

Check out name hiding. http://www.parashift.com/c++-faq-lit....html#faq-23.9
Dec 20 '07 #2
On Dec 20, 4:42 pm, Spoon <root@localhostwrote:
Hello,

Consider the following code.

$ cat foo.C
#include <cstdio>

struct A
{
int bar() { puts("A::bar()"); return 666; }
int Abar() { puts("A::bar()"); return 666; }

};

struct B : public A
{
void bar(int n) { printf("B::bar(%d)\n", n); }

};

int main()
{
B foo;
foo.bar(123);
foo.bar();
foo.A::bar();
foo.Abar();
return 0;

}

Both A and B provide a method 'bar' but the signature is different.
int bar(void) in A
void var(int) in B

I have an object of type B, and I want to call A's bar.

The ovious foo.bar() is considered a syntax error by GCC.

$ g++ -Wall -Wextra -std=c++98 -pedantic foo.C
foo.C: In function `int main()':
foo.C:18: error: no matching function for call to `B::bar()'
foo.C:11: note: candidates are: void B::bar(int)

Does this mean I either have to change the name of A's bar as in Abar,
or qualify bar as in A::bar? Are there other alternatives?

Regards.
Redefining base class member function in the derived class, hides all
the implementation of the base class members. You could use,

foo.A::bar();
Dec 20 '07 #3

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

Similar topics

2
by: Shailan | last post by:
Hi Im having trouble with the following code that seems to be behave differently when called from the browser as opposed to the command line. The calling script is a cgi that forks, with the...
4
by: Paul | last post by:
Hi, In SQL Books Online in the section on @@Error it gives the following example: -- Execute the INSERT statement. INSERT INTO authors (au_id, au_lname, au_fname, phone, address, city,...
1
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
17
by: Bill Grigg | last post by:
I have been successfully calling DLL's using VC++ 6.0 and also using VC++7.1 (.NET). I only mention this because I have never felt comfortable with the process, but nonetheless it did work....
1
by: desmcc | last post by:
Hi, I am launching a modal dialog through the usual javascript (window.showmodaldialog). When the modal dialog is complete (ie user selects OK), the calling page then refreshes itself by setting...
0
by: Francois Guerin | last post by:
I have an application calling a web service on a different machine. The application code runs under a local user account on the application server. The account (identical username/password) is...
8
by: Duffcase | last post by:
Hi. I'm having some problems consuming a webservice. The server keeps complaining about the wrong element beeing sent.. I have this method that takes two strings String_1 and String_2 The...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.