By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Shadowing a parameter

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.