Groups | Blog | Home
all groups > visual studio .net general > september 2005 >

visual studio .net general : VS2005 C++ compile error


Jonathan Potter
9/23/2005 12:00:00 AM
Hi Gary, thanks for the reply but I think you might have missed the actual
point.

The error is not coming from trying to access the private section of a base
class from a derived class.

The error is coming from a function in a derived class having a parameter
that is the type of a private base class. Surely the C++ standard doesn't
say you can't pass instances of private base classes to functions in derived
classes?

Please see my example code again.

Thanks,
Jonathan Potter

[quoted text, click to view]

Jonathan Potter
9/23/2005 12:00:00 AM
Hi, I have just installed the VS2005 Beta 2 and found our code would not
compile when it compiled fine in 2003. I've isolated the problem to the
following (sample) construction:

class A
{
};

class B : private A
{
public:
void Func(A& a);
};

class C : public B
{
public:
// error here
void Func(A& a);
}

(ignore the fact that this does nothing)

The error generated is "error C2247: 'A' not accessible because 'B' uses
'private' to inherit from 'A'".

As far as I can see there is nothing wrong with the above construction. Is
this a compiler error?

Thanks,
Jon

v-garych NO[at]SPAM online.microsoft.com (
9/23/2005 5:54:57 AM
Hi Jonathan,

[quoted text, click to view]

I think the VS2005 C++ compiler's behavior is correct, according to the C++
standard, anything placed within a private section of the base class is
only available to the class itself, and not to any of the derived classes,
also if the base class is privately inherited, then the base class could be
only accessible to the class itself(in your case: B), and could not be
accessible to its derived class(in your case: C).

I am afraid the VS2003 C++ compiler doesn't comply to the C++ language
standard strictly in this point.


Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
v-garych NO[at]SPAM online.microsoft.com (
9/23/2005 8:08:55 AM
Hi Jonathan,

[quoted text, click to view]

yes, I know your concern on this issue, based on my understanding, since
the class B inherites class A privately, so only class B could access its
base class's instance--the member of its private base class is onle
accessible to itself.

So I think the real problem is why you can't define a function in a derived
class(C) having a parameter *which type* is the class's(B) private base
class(A), I found one workaround to this scenario is to declare the class A
again just before the class C:

...
class A;

class C : public B
{
public:
// error here
void Func(A& a);
}


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
v-garych NO[at]SPAM online.microsoft.com (
9/24/2005 12:00:00 AM
Hi Jonathan,

I have also consulted this issue with our VC product team members, they
confirmed this is not a problem, that's the way C++ works. The compiler
finds the base class A first, and that's its(C's) view of A.

Another more standard convention in this scenario is as following, without
the redeclaration:

void Func(::A& a);


Hope this helps!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
Jonathan Potter
10/11/2005 12:00:00 AM
Thank you Gary, yes I understand this now.

I assume the fact that it compiled in VC++ 6.0 was a mistake then?

Cheers,
Jon

[quoted text, click to view]

v-garych NO[at]SPAM online.microsoft.com (
10/11/2005 12:00:00 AM
[quoted text, click to view]

I don't think so, the difference is the new VC++ compiler complies to the
C++ language standard more strictly than the previous ones...


Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button