I've noticed what I think is a bug in VC++ 6.0. Can someone confirm and
also test whether it is still present in later versions?

In C++, as in C, it's legal to use the same name for both a user-defined
type and a function or object. When the type name is referenced, if it
is, it must be preceded by the keyword for its declaration.

It is also legal to forward-declare a class name in a friend statement,
making an as-yet-undefined class a friend of the current class.

The following code should be legal but instead VC++ 6.0 produces an
error message.

class A {
private:
int x;
void a(); // declares member function
friend class a; // forward declares a as a class and friend
};
class a{
public:
a(A &aa) {++aa.x} // reference to private member should be allowed
// because a is a friend of A
};

The error occurs in the constructor of class a with the message "'x' :
cannot access private member declared in class 'A'".

If I move the friend statement above the declaration of function a(), or
if I forward declare class a before class A the code compiles without error.

Norm
--
--
To reply, change domain to an adult feline.

Re: Bug in VC++ - Conflict between function name and class name by Carl

Carl
Wed Apr 20 22:45:59 CDT 2005

Norman Bullen wrote:
> I've noticed what I think is a bug in VC++ 6.0. Can someone confirm
> and also test whether it is still present in later versions?
>
> In C++, as in C, it's legal to use the same name for both a
> user-defined type and a function or object. When the type name is
> referenced, if it is, it must be preceded by the keyword for its
> declaration.
>
> It is also legal to forward-declare a class name in a friend
> statement, making an as-yet-undefined class a friend of the current
> class.
> The following code should be legal but instead VC++ 6.0 produces an
> error message.
>
> class A {
> private:
> int x;
> void a(); // declares member function
> friend class a; // forward declares a as a class and friend
> };
> class a{
> public:
> a(A &aa) {++aa.x} // reference to private member should be allowed
> // because a is a friend of A
> };
>
> The error occurs in the constructor of class a with the message "'x' :
> cannot access private member declared in class 'A'".
>
> If I move the friend statement above the declaration of function a(),
> or if I forward declare class a before class A the code compiles without
> error.

I might point out that VC6 is A) nearly 7 years old and B) no longer
supported.

Second, your description of forward declaration of a friend is correct -
this is a bug, and it's still present in VC7.1.

I'm unable to test VC8 beta 2 at the moment (in the middle of installation
actually), but it might be fixed there.

A workaround (and I'd propose better style anyway) is to forward declare
class a before the definition of class A.

Lastly, you're missing a semicolon to make the code legal.

-cd



Re: Bug in VC++ - Conflict between function name and class name by Carl

Carl
Wed Apr 20 23:56:18 CDT 2005

Carl Daniel [VC++ MVP] wrote:
> I'm unable to test VC8 beta 2 at the moment (in the middle of
> installation actually), but it might be fixed there.

This bug is still present in VC8 beta 2 (14.00.50215.44).

You might want to open a bug report on

http://lab.msdn.microsoft.com/productfeedback/

-cd



Re: Bug in VC++ - Conflict between function name and class name by Gene

Gene
Thu Apr 21 13:19:05 CDT 2005

"Norman Bullen" <norm@BlackKittenAssociates.com.INVALID> wrote in message
news:buE9e.9592$yq6.1084@newsread3.news.pas.earthlink.net...
...
>
> It is also legal to forward-declare a class name in a friend statement,
> making an as-yet-undefined class a friend of the current class.

For the sake of precision, friend class declaration is not a forward
declaration. But special lookup rules make it somewhat similar(11.4/9): "For
a friend class declaration, if there is no prior declaration, the class that
is specified belongs to the innermost
enclosing non-class scope, but if it is subsequently referenced, its name is
not found by name lookup until a
matching declaration is provided in the innermost enclosing nonclass scope."
Unlike forward declaration, friend declaration doesn't introduce the class
name into the current scope.
There is a simple workaround for the lookup bug, just forward declare your
to be friend class in the innermost enclosing non-class scope.

Gene



Re: Bug in VC++ - Conflict between function name and class name by Norman

Norman
Thu Apr 21 20:29:12 CDT 2005

Gene Bushuyev wrote:
> "Norman Bullen" <norm@BlackKittenAssociates.com.INVALID> wrote in message
> news:buE9e.9592$yq6.1084@newsread3.news.pas.earthlink.net...
> ...
>
>>It is also legal to forward-declare a class name in a friend statement,
>>making an as-yet-undefined class a friend of the current class.
>
>
> For the sake of precision, friend class declaration is not a forward
> declaration. But special lookup rules make it somewhat similar(11.4/9): "For
> a friend class declaration, if there is no prior declaration, the class that
> is specified belongs to the innermost
> enclosing non-class scope, but if it is subsequently referenced, its name is
> not found by name lookup until a
> matching declaration is provided in the innermost enclosing nonclass scope."
> Unlike forward declaration, friend declaration doesn't introduce the class
> name into the current scope.
> There is a simple workaround for the lookup bug, just forward declare your
> to be friend class in the innermost enclosing non-class scope.
>
> Gene
>
>
Thanks for that clarification. As mentioned above, I already found this
workaround.

Norm

--
--
To reply, change domain to an adult feline.