Microsoft says...

U

Uenal Mutlu

The documentation of Microsoft's latest C++ compiler version
(it's called ".NET 2005" or something that) says:

// QUOTE START
Summary of Compile-Time Breaking Changes

This topic summarizes the compile-time errors and
warnings that will now be issued on code that compiled
without errors or warnings prior to Visual C++ 2003.
However, some conformance-related, compile-time breaking
changes were introduced in Visual C++ 2005.

A protected member (n) can only be accessed through a
member function of a class (B) that inherits from the
class (A) of which it (n) is a member (C2247).

' identifier ' not accessible because ' class ' uses ' specifier ' to inherit
from ' class '

identifier is inherited from a class declared with private or protected access.

The following sample generates C2247:

// C2247.cpp
class A {
public:
int i;
};
class B : private A {}; // B inherits a private A
class C : public B {} c; // so even though C's B is public
int j = c.i; // C2247, i not accessible

// QUOTE END

So, are they telling the C++ programmers who have to use their product
that their new compiler now detects this error? And that all their old
versions of the last maybe 10 years did not detect this basic error?
 
I

Ioannis Vranos

Uenal said:
The documentation of Microsoft's latest C++ compiler version
(it's called ".NET 2005" or something that) says:

// QUOTE START
Summary of Compile-Time Breaking Changes

This topic summarizes the compile-time errors and
warnings that will now be issued on code that compiled
without errors or warnings prior to Visual C++ 2003.
However, some conformance-related, compile-time breaking
changes were introduced in Visual C++ 2005.

A protected member (n) can only be accessed through a
member function of a class (B) that inherits from the
class (A) of which it (n) is a member (C2247).

' identifier ' not accessible because ' class ' uses ' specifier ' to inherit
from ' class '

identifier is inherited from a class declared with private or protected access.

The following sample generates C2247:

// C2247.cpp
class A {
public:
int i;
};
class B : private A {}; // B inherits a private A
class C : public B {} c; // so even though C's B is public
int j = c.i; // C2247, i not accessible

// QUOTE END

So, are they telling the C++ programmers who have to use their product
that their new compiler now detects this error? And that all their old
versions of the last maybe 10 years did not detect this basic error?


VC++ 2003 with just

cl temp.cpp

doesn't compile it:


temp.cpp
temp.cpp(13) : error C2247: 'A::i' not accessible because 'B' uses 'private' to
inherit from 'A'
temp.cpp(4) : see declaration of 'A::i'
temp.cpp(6) : see declaration of 'B'
temp.cpp(2) : see declaration of 'A'
 
M

msalters

Uenal said:
The documentation of Microsoft's latest C++ compiler version
(it's called ".NET 2005" or something that) says:

// QUOTE START
Summary of Compile-Time Breaking Changes

This topic summarizes the compile-time errors and
warnings that will now be issued on code that compiled
without errors or warnings prior to Visual C++ 2003.
However, some conformance-related, compile-time breaking
changes were introduced in Visual C++ 2005.

A protected member (n) can only be accessed through a
member function of a class (B) that inherits from the
class (A) of which it (n) is a member (C2247).

The following sample generates C2247:

// C2247.cpp
class A {
public:
int i;
};
class B : private A {}; // B inherits a private A
class C : public B {} c; // so even though C's B is public
int j = c.i; // C2247, i not accessible

// QUOTE END

So, are they telling the C++ programmers who have to use their product
that their new compiler now detects this error? And that all their old
versions of the last maybe 10 years did not detect this basic error?

No, they're not. You're reading it wrong. There are a number of access
checks. The breaking change refers to a specific case, which applies
only to protected. The sample is anoter case which generates the same
message. The sample doesn't even /use/ protected!

The not-so-basic error is IIRC:

class A {
protected:
int i;
};
class B : A {
void foo(A* that) {
this->i; // OK
that->i; // Not OK, C2247 in VC8
}
};

HTH,
Michiel Salters
 
D

Duane Hebert

msalters said:
The not-so-basic error is IIRC:

class A {
protected:
int i;
};
class B : A {
void foo(A* that) {
this->i; // OK
that->i; // Not OK, C2247 in VC8
}
};

With VC++ 2005 Express Beta I get the same. Also
with Borland's CBX. I understand why that->i
doesn't work. Why does this->i work? B is derived
using private inheritance. Shouldn't that make this->i
inaccessible? If not, how does private inheritance work?
I admit that I've never used it but...
 
R

Ron Natalie

Duane said:
With VC++ 2005 Express Beta I get the same. Also
with Borland's CBX. I understand why that->i
doesn't work. Why does this->i work? B is derived
using private inheritance. Shouldn't that make this->i
inaccessible? If not, how does private inheritance work?
I admit that I've never used it but...
Of course it should work, that's the whole point of making
it protected, the derived parts of the class can get at the
protected base parts.
 
M

msalters

Duane said:
With VC++ 2005 Express Beta I get the same. Also
with Borland's CBX. I understand why that->i
doesn't work. Why does this->i work? B is derived
using private inheritance. Shouldn't that make this->i
inaccessible?

No. private inheritance makes the public and protected members of
A accesible to B, but not to C - even if C inherits from B. So,
even if C indirectly inherits from A it can't access the protected
members of A. B can still use its private bases just like its
private members. Private inheritance, like private members, do not
affect the class in which the private appears.

HTH,
Michiel Salters
 
D

Duane Hebert

Of course it should work, that's the whole point of making
it protected, the derived parts of the class can get at the
protected base parts.

Yep. I opened a book <g> Using private inheritance,
doesn't affect the relation between A and B but it makes
the public and protected members derived from A private
wrt any class deriving from B. That's the part that I forgot.
 
D

Duane Hebert

No. private inheritance makes the public and protected members of
A accesible to B, but not to C - even if C inherits from B. So,
even if C indirectly inherits from A it can't access the protected
members of A. B can still use its private bases just like its
private members. Private inheritance, like private members, do not
affect the class in which the private appears.

Yep. I looked in one of my books after I posted (should do that before -
sorry for the noise<g>)
 
I

Ioannis Vranos

Duane said:
Yep. I looked in one of my books after I posted (should do that before -
sorry for the noise<g>)


To make things easier to remember, you can say that private inheritance makes public and
protected members private, and protected inheritance makes them protected.
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top