Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Attn to All. Problem with Access Modifier.

ignatandrignatandr Member Posts: 10
This is the code sample
class A
{
public:
int a;
};

class B : public A
{
private:
A :: a;
};

int main()
{
B b;
b.a = 10;
cout << b.a << endl;

return 0;
}
Now, Can anyone tell me why this compiles with no problem on Linux Mandrake8.0 using g++ v. 2.95.3.
It gives me errors on Visual C++.

Compiling...
error C2248: 'a' : cannot access private member
error C2248: 'a' : cannot access private member
Error executing cl.exe.
Thanks.


Comments

  • abcabc Member Posts: 443
    [b][red]This message was edited by abc at 2002-9-27 18:23:18[/red][/b][hr]
    : This is the code sample
    : class A
    : {
    : public:
    : int a;
    : };
    :
    : class B : public A
    : {
    : private:
    : A :: a;
    : };
    :
    : int main()
    : {
    : B b;
    : b.a = 10;
    : cout << b.a << endl;
    :
    : return 0;
    : }
    : Now, Can anyone tell me why this compiles with no problem on Linux Mandrake8.0 using g++ v. 2.95.3.
    : It gives me errors on Visual C++.
    :
    : Compiling...
    : error C2248: 'a' : cannot access private member
    : error C2248: 'a' : cannot access private member
    : Error executing cl.exe.
    : Thanks.

    The C++ standard seems to regard [blue]access declarations[/blue] as extending rather than restricting access. It doesn't say so explicitly, but judging from the fact that they are defined to be equivalent to [blue]using[/blue] declarations ... anyway, restricting the access is done by the access specifier of the inherited base class (i.e. make that [italic]class B: private A {...}[/italic].

    see this for the standard specs:
    http://www.kuzbass.ru:8086/docs/isocpp/access.html#class.access.dcl


  • DariusDarius Member Posts: 1,666
    : [b][red]This message was edited by abc at 2002-9-27 18:23:18[/red][/b][hr]
    : : This is the code sample
    : : class A
    : : {
    : : public:
    : : int a;
    : : };
    : :
    : : class B : public A
    : : {
    : : private:
    : : A :: a;
    : : };
    : :
    : : int main()
    : : {
    : : B b;
    : : b.a = 10;
    : : cout << b.a << endl;
    : :
    : : return 0;
    : : }
    : : Now, Can anyone tell me why this compiles with no problem on Linux Mandrake8.0 using g++ v. 2.95.3.
    : : It gives me errors on Visual C++.
    : :
    : : Compiling...
    : : error C2248: 'a' : cannot access private member
    : : error C2248: 'a' : cannot access private member
    : : Error executing cl.exe.
    : : Thanks.
    :
    : The C++ standard seems to regard [blue]access declarations[/blue] as extending rather than restricting access. It doesn't say so explicitly, but judging from the fact that they are defined to be equivalent to [blue]using[/blue] declarations ... anyway, restricting the access is done by the access specifier of the inherited base class (i.e. make that [italic]class B: private A {...}[/italic].
    :
    : see this for the standard specs:
    : http://www.kuzbass.ru:8086/docs/isocpp/access.html#class.access.dcl

    That and access declarations are deprecated.


    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion

  • Shawn CarterShawn Carter Member Posts: 0

    _____ | http://forcoder.org | free video tutorials and ebooks about || C++, Python, MATLAB, Java, Assembly, Visual Basic .NET, PHP, PL/SQL, Ruby, Perl, C#, Swift, Go, Visual Basic, R, Delphi, JavaScript, Scratch, C, Objective-C Hack, Scala, LabVIEW, Lua, Lisp, Crystal, Ada, F#, Scheme, Awk, Transact-SQL, Prolog, ABAP, Fortran, Kotlin, COBOL, Dart, VBScript, Clojure, Bash, Erlang, Rust, FoxPro, Apex, Alice, ML, Logo, D, Julia, SAS || ____________

Sign In or Register to comment.