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

Shadowing a parameter

Hey all. I was making a newbie mistake that I eventually figured out. That
is not my question. My question is about the error message. So let me set
the stage for you:

class Superclass
{
public:
Superclass(int);
}

class Subclass : public Superclass
{
public:
Subclass(int);
}

As you all surely know, the way to declare the constructor of subclass so
that it uses that of superclass is

Subclass::Subclass(int x)
: Superclass(x)
{ . . . }

As a newbie who comes from a Java background, I somehow managed to miss this
colon initializer notation in my text (B.Stoustrup or however you spell it).
So I was writing it as any Java newbie would:

Subclass::Subclass(int x)
{ Superclass::Superclass(x); . . . }

The real question, though, is why the heck can't the error message be more
useful:

"Error: declaration of x shadows parameter"

What in the world does that mean? What is "shadowing" a parameter. And
where do I declare something that does so? As far as I can see, the
parameter itself is the only declaration of any kind. This is from g++ for
Cygwin, whatever the most recent version is.

Yours,

James
Jul 22 '05 #1
3 9439
Aguilar, James wrote in news:cf**********@newsreader.wustl.edu in
comp.lang.c++:
Subclass::Subclass(int x)
{ Superclass::Superclass(x); . . . }

The real question, though, is why the heck can't the error message be
more useful:

"Error: declaration of x shadows parameter"
It is useful, its telling you what is *really* wrong with your code.

What in the world does that mean? What is "shadowing" a parameter.
In C++ there is a rule:

If some thing can be parsed as a declaration it is.

In this case "Superclass::Superclass" is type and the brackets around
the "x" don't matter, its as if you wrote:

Superclass x;

I.e declared a local variable x of type Superclass. It "shadows" the
paramiter declaration "int x".
And where do I declare something that does so? As far as I can see,
the parameter itself is the only declaration of any kind. This is
from g++ for Cygwin, whatever the most recent version is.


The error you got was nothing to do with not knowing how to invoke
base class constructors. It was about not knowing the declaration
rules for C++.

Its an unfortunate fact that often error messages don't reflect
the *real* problem, but some (possibly unrelated) side effect
of that which we did wrong.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Aguilar, James wrote:
Hey all. I was making a newbie mistake that I eventually figured out.
That is not my question. My question is about the error message. So
let me set the stage for you:

class Superclass
{
public:
Superclass(int);
}

class Subclass : public Superclass
{
public:
Subclass(int);
}

As you all surely know, the way to declare the constructor of subclass
so that it uses that of superclass is

Subclass::Subclass(int x)
: Superclass(x)
{ . . . }

As a newbie who comes from a Java background, I somehow managed to
miss this colon initializer notation in my text (B.Stoustrup or
however you spell it). So I was writing it as any Java newbie would:

Subclass::Subclass(int x)
{ Superclass::Superclass(x); . . . }

The real question, though, is why the heck can't the error message be
more useful:
Because the compiler only sees what you wrote, not what you intended.
"Error: declaration of x shadows parameter"

What in the world does that mean? What is "shadowing" a parameter.
It means that you declare something with the same name as the parameter
and so you cannot use that name to accesss the parameter anymore, like:

void foo(int x)
{
int x;
// now the local x shadows the parameter x
}
And where do I declare something that does so?
Superclass::Superclass(x);

is the same as:

Superclass (x);

and the same as:

Superclass x;
As far as I can see, the parameter itself is the only declaration of
any kind.
It's not a parameter. It's a local variable.
This is from g++ for Cygwin, whatever the most recent version is.


Jul 22 '05 #3
Aguilar, James wrote:
Hey all. I was making a newbie mistake that I eventually figured out. That
is not my question. My question is about the error message. So let me set
the stage for you:

class Superclass
{
public:
Superclass(int);
}

class Subclass : public Superclass
{
public:
Subclass(int);
}
Don't forget the semicolons after the class declarations.

As you all surely know, the way to declare the constructor of subclass so
that it uses that of superclass is

Subclass::Subclass(int x)
: Superclass(x)
{ . . . }

As a newbie who comes from a Java background, I somehow managed to miss this
colon initializer notation in my text (B.Stoustrup or however you spell it).
So I was writing it as any Java newbie would:

Subclass::Subclass(int x)
{ Superclass::Superclass(x); . . . }

The real question, though, is why the heck can't the error message be more
useful:
The code is erroneous, but not for the reason you expect.

Note that Superclass is certainly within the Superclass scope that you
specify with the scope resolution operator ::. So
Superclass::Superclass refers to the class Superclass. Then note that
the grammar permits declarators to be surrounded by parenthesis. So IOW,

Superclass::Superclass(x);

just means
Superclass x;

so you're defining an instance of Superclass in the constructor with
identifier x, which would ordinarily be fine. However, there's a
parameter with the same identifier, and since they have the same scope
(more or less) you get the error:

"Error: declaration of x shadows parameter"


[...]

-Peter
Jul 22 '05 #4

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

Similar topics

3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
2
by: Mark Sisson | last post by:
Hi all. SITUATION ================ 1. I have a base class with a member variable that's an object 2. I have several classes that inherit from the base class. 3. There are several methods in...
3
by: Nick Stansbury | last post by:
Hi, Quick (and probably simple) question regarding shadowing and polymorphism. Problem is best explained by an example. Public Class A public CommonProperty as Integer end class Public...
5
by: Dave Taylor | last post by:
I'm trying to derive a class from a typed DataSet to add some methods to one of the DataTables. I would like to keep the names the same in the derived class as in the base class, so I have used...
2
by: David Garamond | last post by:
Is there a feature similar to this currently in Postgres, or will there be? Sometimes (like in a shared hosting environment), we cannot have the luxury of hot-swapped RAID or expensive SAN, and...
1
by: John Salerno | last post by:
I understand that if you reassign a built-in name to your own variable, such as: str = 'hello' then you lose the use of the built-in (in this case str()), but is this also the case in terms...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
11
by: wuzertheloser | last post by:
Write a program which calculates the integral of the function f(x)=(A*x^m)/n! on the interval from a to b (0<a<b). In the main program, scanf a double value for m and a...
3
by: Cartoper | last post by:
I really like the shadowing that this web site is using behind the images. I have tried to dig through their code to figure out how they did it, but it still eludes me. How do they do it? ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: 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)...

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.