Re: Sanity check: public/private
Carlos Moreno wrote:
Say that I have a C++ program that consists only of perfectly
valid C++ code (in other words, the program compiles and does
not invoke undefined behaviour).
If I now replace every single instance of the keyword private
with the keyword public (and I change *nothing else*), would
the behaviour of my program change? That is, in practical
terms, should I expect the behaviour of my program to change
when compiled with an *actual* compiler that is reasonably
compliant and reasonably high-quality?
My guts say no. Since private/public governs accessibility and not
visibility, you will not introduce any changes into the name lookup so the
resulting calls should be the same. This is for functions though.
I could however imagine the code to not compile in some cases. Consider
this:
class A{};
class B{};
void foo( A*);
void foo( B*);
class C:
public A,
private B
{};
C c;
foo(&c);
'&c' will yield a C* which can be converted to an A* but not to a B*, so the
only foo() overload in question would be the one taking an A*. Making B a
public baseclass would now introduce an ambiguity.
Note: I didn't compile this and I'm honestly not sure if it would make a
difference, i.e. if the ambiguity wouldn't already exist with the private
baseclass. This would be consistent with the way functions are handled,
i.e. that visibility is separate from accessibility.
In all of the above: should I expect the generated machine
code to change? (assuming all the compiler switches and
optimization settings are exactly the same)
Yes, I know for example that MSC mangles the access specifier into the
function name.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]