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

why this wont compile?

---------

typedef struct {int i;} somestruct;

class A
{
public:
A() {}
bool read(char* ptr, int len);
// bool read2(char* ptr, int len); // 1.
};

class B : public A
{
public:
B() {}
bool read(somestruct* s) {return read((char*)s, (int)sizeof(*s));}
// bool read(somestruct* s) {return read2((char*)s, (int)sizeof(*s));} // 1.
// bool read(somestruct* s) {return A::read((char*)s, (int)sizeof(*s));} //
2.
};

----------

A::read was inherited and should be in scope with B::read, but the compiler
still can't find it. I have even casted the second argument to int, but it
didn't help either, hehe... If I rename A::read to A::read2 and call read2
(1), or if I call A::read(...) by specifying the scope (2) then it works,
but it is pretty inconvinient and unnecessarily looking to my opinion. I
suspect there is a standard compliance behind this, but what is the reason
disallowing this kind of code? And why is it case so different from the
following piece of (compiling) code? (please ignore that ptr is pointing to
nowhere :)

----------

typedef struct {int i;} somestruct;

bool read(char* ptr, int len);
bool read(somestruct* s) {return read((BYTE*)s, sizeof(*s));}

void somefunct()
{
char* ptr;
read(ptr, 1);
somestruct s;
read(&s);
}

----------
Nov 16 '05 #1
2 1008
Because C++ uses "hide by name" rules instead of "hide by signature". The
definition of read in class B makes all definitions of read in base classes
hidden.

Use a "using declaration" to get the behavior you want.

class B : public A
{
public:
B() {}
using A::read;
bool read(somestruct* s) {return read((char*)s, (int)sizeof(*s));
}

Ronald Laeremans
Visual C++ team
"Gabest" <ga****@freemail.hu> wrote in message
news:ez**************@TK2MSFTNGP12.phx.gbl...
---------

typedef struct {int i;} somestruct;

class A
{
public:
A() {}
bool read(char* ptr, int len);
// bool read2(char* ptr, int len); // 1.
};

class B : public A
{
public:
B() {}
bool read(somestruct* s) {return read((char*)s, (int)sizeof(*s));}
// bool read(somestruct* s) {return read2((char*)s, (int)sizeof(*s));} // 1. // bool read(somestruct* s) {return A::read((char*)s, (int)sizeof(*s));} // 2.
};

----------

A::read was inherited and should be in scope with B::read, but the compiler still can't find it. I have even casted the second argument to int, but it
didn't help either, hehe... If I rename A::read to A::read2 and call read2
(1), or if I call A::read(...) by specifying the scope (2) then it works,
but it is pretty inconvinient and unnecessarily looking to my opinion. I
suspect there is a standard compliance behind this, but what is the reason
disallowing this kind of code? And why is it case so different from the
following piece of (compiling) code? (please ignore that ptr is pointing to nowhere :)

----------

typedef struct {int i;} somestruct;

bool read(char* ptr, int len);
bool read(somestruct* s) {return read((BYTE*)s, sizeof(*s));}

void somefunct()
{
char* ptr;
read(ptr, 1);
somestruct s;
read(&s);
}

----------

Nov 16 '05 #2
"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message news:<Oz**************@TK2MSFTNGP10.phx.gbl>...
Because C++ uses "hide by name" rules instead of "hide by signature". The
definition of read in class B makes all definitions of read in base classes
hidden.

Use a "using declaration" to get the behavior you want.

class B : public A
{
public:
B() {}
using A::read;
bool read(somestruct* s) {return read((char*)s, (int)sizeof(*s));
}

Ronald Laeremans
Visual C++ team


Thanks, I had absolutely no idea about this "using" keyword before! I
guess it is never late to learn something new.
Nov 16 '05 #3

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

Similar topics

3
by: penguinman | last post by:
Its just a hello world program. Compile and build goes with no warning or error. i type ./a.out and the shell just sits there. It doesnt hang but it just sits there with no output. I executed...
11
by: penguinman | last post by:
I type ./a.out and the shell just sits there. It doesnt hang it just sits there looking pretty. I run the program with the ddd option and executed it from ddd...execution window comes up and...
2
by: Zardoz | last post by:
Hi, I'm trying to find a workaround to this problem ON_BN_CLICKED(IDC_CANCEL, OnCancelOperation) BOOL CCurrenciesDialog::OnCancelOperation()
5
by: manokumar | last post by:
hiye, i notice that some if not all of my folders in winxp pro. are set as read only and its giving me some problem with development. so as the natural thing, i unchecked the read only option and...
4
by: dhnriverside | last post by:
HI guys I've just written my first independent namespace for my library (yay me!). However, on trying to add it to my website project, it causes an error when I look at the website. It compiles...
16
by: lumpybanana247 | last post by:
this will compile with no errors, debug no errors- just doesnt really do any thing when it runs #include <iostream> using namespace std; void calculate(int a,int b,int& c); //you need to...
2
by: kal | last post by:
I have registered my custom control in web.config as follows: <add tagPrefix="PhoenixControls" namespace="PhoenixControls" assembly="PhoenixControls"/> I have added a file called...
3
by: =?Utf-8?B?SlA=?= | last post by:
<asp:GridView ID="gridResults" runat="server" AutoGenerateColumns="False" Width="98%" PageSize="25" AllowPaging="True" OnSorting="gridResults_Sorting" OnPageIndexChanging...
0
by: alan75 | last post by:
After i've build the exe files and everything, I copied all files to another PC, remembering to also copy the required DLLs and registering them. The error message when trying to run the program...
7
by: H1TM4N | last post by:
can someone help? Its the following code it just wont compile thanks :) PROGRAM Store (INPUT, OUTPUT); VAR Change, AmountPaid, TotalPrice :REAL; IntChange, Dollars, Quarters, Dimes,...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.