Shadowing a parameter

A

Aguilar, James

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
 
R

Rob Williscroft

Aguilar, James wrote in 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.
 
R

Rolf Magnus

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.
 
P

Peter Ammon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top