access to private members of internal class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shea Martin

    access to private members of internal class

    Class A
    {
    public:
    class B;
    int funct(const B &b);
    };

    Class A::B
    {
    private:
    int _member;
    };

    int A::funct(const A::B &b)
    {
    return 10*b._member; //won't compile
    }

    ////////////////////////////
    The above code will not compile, as I get a error to the effect of
    member _member not accessable from A::funct(const A::B&)

    Is there anyway around this, short of declaring _member to be public?

    Thanks,

    ~S
  • John Harrison

    #2
    Re: access to private members of internal class


    "Shea Martin" <smartin@arcis. com> wrote in message
    news:Yljdc.4779 $G3.43042@local host...[color=blue]
    > Class A
    > {
    > public:
    > class B;
    > int funct(const B &b);
    > };
    >
    > Class A::B
    > {
    > private:
    > int _member;
    > };
    >
    > int A::funct(const A::B &b)
    > {
    > return 10*b._member; //won't compile
    > }
    >
    > ////////////////////////////
    > The above code will not compile, as I get a error to the effect of
    > member _member not accessable from A::funct(const A::B&)
    >
    > Is there anyway around this, short of declaring _member to be public?
    >
    > Thanks,
    >
    > ~S[/color]

    Make A a friend of B.

    class A::B
    {
    friend class A;
    ...

    john


    Comment

    • John Harrison

      #3
      Re: access to private members of internal class


      "Shea Martin" <smartin@arcis. com> wrote in message
      news:Yljdc.4779 $G3.43042@local host...[color=blue]
      > Class A
      > {
      > public:
      > class B;
      > int funct(const B &b);
      > };
      >
      > Class A::B
      > {
      > private:
      > int _member;
      > };
      >
      > int A::funct(const A::B &b)
      > {
      > return 10*b._member; //won't compile
      > }
      >
      > ////////////////////////////
      > The above code will not compile, as I get a error to the effect of
      > member _member not accessable from A::funct(const A::B&)
      >
      > Is there anyway around this, short of declaring _member to be public?
      >
      > Thanks,
      >
      > ~S[/color]

      Make A a friend of B.

      class A::B
      {
      friend class A;
      ...

      john


      Comment

      • Leor Zolman

        #4
        Re: access to private members of internal class

        On Thu, 8 Apr 2004 22:24:00 +0100, "John Harrison"
        <john_andronicu s@hotmail.com> wrote:
        [color=blue]
        >
        >"Shea Martin" <smartin@arcis. com> wrote in message
        >news:Yljdc.477 9$G3.43042@loca lhost...[color=green]
        >> Class A
        >> {
        >> public:
        >> class B;
        >> int funct(const B &b);
        >> };
        >>
        >> Class A::B
        >> {
        >> private:
        >> int _member;
        >> };
        >>
        >> int A::funct(const A::B &b)
        >> {
        >> return 10*b._member; //won't compile
        >> }
        >>
        >> ////////////////////////////
        >> The above code will not compile, as I get a error to the effect of
        >> member _member not accessable from A::funct(const A::B&)
        >>
        >> Is there anyway around this, short of declaring _member to be public?
        >>
        >> Thanks,
        >>
        >> ~S[/color]
        >
        >Make A a friend of B.
        >
        >class A::B
        >{
        > friend class A;
        > ...
        >
        >john[/color]

        Alternatively, /if/ A's functions don't need to /change/ the value of
        _member, then having A::B provide a public accessor function would be
        another possibility. (But that's a big "if", since this is evidently not
        the code Shea actually tried to compile.)
        -leor
        [color=blue]
        >[/color]

        --
        Leor Zolman --- BD Software --- www.bdsoft.com
        On-Site Training in C/C++, Java, Perl and Unix
        C++ users: Download BD Software's free STL Error Message Decryptor at:
        An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

        Comment

        • Leor Zolman

          #5
          Re: access to private members of internal class

          On Thu, 8 Apr 2004 22:24:00 +0100, "John Harrison"
          <john_andronicu s@hotmail.com> wrote:
          [color=blue]
          >
          >"Shea Martin" <smartin@arcis. com> wrote in message
          >news:Yljdc.477 9$G3.43042@loca lhost...[color=green]
          >> Class A
          >> {
          >> public:
          >> class B;
          >> int funct(const B &b);
          >> };
          >>
          >> Class A::B
          >> {
          >> private:
          >> int _member;
          >> };
          >>
          >> int A::funct(const A::B &b)
          >> {
          >> return 10*b._member; //won't compile
          >> }
          >>
          >> ////////////////////////////
          >> The above code will not compile, as I get a error to the effect of
          >> member _member not accessable from A::funct(const A::B&)
          >>
          >> Is there anyway around this, short of declaring _member to be public?
          >>
          >> Thanks,
          >>
          >> ~S[/color]
          >
          >Make A a friend of B.
          >
          >class A::B
          >{
          > friend class A;
          > ...
          >
          >john[/color]

          Alternatively, /if/ A's functions don't need to /change/ the value of
          _member, then having A::B provide a public accessor function would be
          another possibility. (But that's a big "if", since this is evidently not
          the code Shea actually tried to compile.)
          -leor
          [color=blue]
          >[/color]

          --
          Leor Zolman --- BD Software --- www.bdsoft.com
          On-Site Training in C/C++, Java, Perl and Unix
          C++ users: Download BD Software's free STL Error Message Decryptor at:
          An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

          Comment

          • Shea Martin

            #6
            Re: access to private members of internal class

            John Harrison wrote:[color=blue]
            > "Shea Martin" <smartin@arcis. com> wrote in message
            > news:Yljdc.4779 $G3.43042@local host...
            >[color=green]
            >>Class A
            >>{
            >>public:
            >> class B;
            >> int funct(const B &b);
            >>};
            >>
            >>Class A::B
            >>{
            >>private:
            >> int _member;
            >>};
            >>
            >>int A::funct(const A::B &b)
            >>{
            >> return 10*b._member; //won't compile
            >>}
            >>
            >>////////////////////////////
            >>The above code will not compile, as I get a error to the effect of
            >>member _member not accessable from A::funct(const A::B&)
            >>
            >>Is there anyway around this, short of declaring _member to be public?
            >>
            >>Thanks,
            >>
            >>~S[/color]
            >
            >
            > Make A a friend of B.
            >
            > class A::B
            > {
            > friend class A;
            > ...
            >
            > john
            >
            >[/color]
            I already tried this, and get this error message:
            "A.h line 4, Error: Iterator is not a structure tag."

            I have tried using Sun's CC 5.0 and 5.6 beta compilers.

            ~S

            Comment

            • Shea Martin

              #7
              Re: access to private members of internal class

              John Harrison wrote:[color=blue]
              > "Shea Martin" <smartin@arcis. com> wrote in message
              > news:Yljdc.4779 $G3.43042@local host...
              >[color=green]
              >>Class A
              >>{
              >>public:
              >> class B;
              >> int funct(const B &b);
              >>};
              >>
              >>Class A::B
              >>{
              >>private:
              >> int _member;
              >>};
              >>
              >>int A::funct(const A::B &b)
              >>{
              >> return 10*b._member; //won't compile
              >>}
              >>
              >>////////////////////////////
              >>The above code will not compile, as I get a error to the effect of
              >>member _member not accessable from A::funct(const A::B&)
              >>
              >>Is there anyway around this, short of declaring _member to be public?
              >>
              >>Thanks,
              >>
              >>~S[/color]
              >
              >
              > Make A a friend of B.
              >
              > class A::B
              > {
              > friend class A;
              > ...
              >
              > john
              >
              >[/color]
              I already tried this, and get this error message:
              "A.h line 4, Error: Iterator is not a structure tag."

              I have tried using Sun's CC 5.0 and 5.6 beta compilers.

              ~S

              Comment

              • Leor Zolman

                #8
                Re: access to private members of internal class

                On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquir rel.ca> wrote:
                [color=blue][color=green]
                >>
                >> Make A a friend of B.
                >>
                >> class A::B
                >> {
                >> friend class A;
                >> ...
                >>
                >> john
                >>
                >>[/color]
                >I already tried this, and get this error message:
                >"A.h line 4, Error: Iterator is not a structure tag."[/color]

                The solution John gave would work for code "such as" that you've shown.

                How can you possibly expect anyone to help you diagnose the cause of that
                error message from the information you've supplied? We don't know what
                Iterator is, or why the compiler would possibly think it is being used as a
                structure tag (um, does it follow the word 'struct'?) But if you showed us
                line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
                guesses...
                -leor

                --
                Leor Zolman --- BD Software --- www.bdsoft.com
                On-Site Training in C/C++, Java, Perl and Unix
                C++ users: Download BD Software's free STL Error Message Decryptor at:
                An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                Comment

                • Leor Zolman

                  #9
                  Re: access to private members of internal class

                  On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquir rel.ca> wrote:
                  [color=blue][color=green]
                  >>
                  >> Make A a friend of B.
                  >>
                  >> class A::B
                  >> {
                  >> friend class A;
                  >> ...
                  >>
                  >> john
                  >>
                  >>[/color]
                  >I already tried this, and get this error message:
                  >"A.h line 4, Error: Iterator is not a structure tag."[/color]

                  The solution John gave would work for code "such as" that you've shown.

                  How can you possibly expect anyone to help you diagnose the cause of that
                  error message from the information you've supplied? We don't know what
                  Iterator is, or why the compiler would possibly think it is being used as a
                  structure tag (um, does it follow the word 'struct'?) But if you showed us
                  line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
                  guesses...
                  -leor

                  --
                  Leor Zolman --- BD Software --- www.bdsoft.com
                  On-Site Training in C/C++, Java, Perl and Unix
                  C++ users: Download BD Software's free STL Error Message Decryptor at:
                  An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

                  Comment

                  • Shea Martin

                    #10
                    Re: access to private members of internal class

                    Leor Zolman wrote:[color=blue]
                    > On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquir rel.ca> wrote:
                    >
                    >[color=green][color=darkred]
                    >>>Make A a friend of B.
                    >>>
                    >>>class A::B
                    >>>{
                    >>> friend class A;
                    >>> ...
                    >>>
                    >>>john
                    >>>
                    >>>[/color]
                    >>
                    >>I already tried this, and get this error message:
                    >>"A.h line 4, Error: Iterator is not a structure tag."[/color]
                    >
                    >
                    > The solution John gave would work for code "such as" that you've shown.
                    >
                    > How can you possibly expect anyone to help you diagnose the cause of that
                    > error message from the information you've supplied? We don't know what
                    > Iterator is, or why the compiler would possibly think it is being used as a
                    > structure tag (um, does it follow the word 'struct'?) But if you showed us
                    > line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
                    > guesses...
                    > -leor
                    >[/color]
                    Iterator is class B. I forgot to modify what I 'pasted'. I greatly apologize to
                    Leor for wasting his precious time, and even more precious thought.

                    I have not used 'friends' very often, and I made the misktake of declaring B to
                    be a friend of A's. Instead of what John had correctly suggested, Making A a
                    friend of B.

                    Thanks,

                    ~S

                    Comment

                    • Shea Martin

                      #11
                      Re: access to private members of internal class

                      Leor Zolman wrote:[color=blue]
                      > On Fri, 09 Apr 2004 01:13:15 GMT, Shea Martin <shea@snowsquir rel.ca> wrote:
                      >
                      >[color=green][color=darkred]
                      >>>Make A a friend of B.
                      >>>
                      >>>class A::B
                      >>>{
                      >>> friend class A;
                      >>> ...
                      >>>
                      >>>john
                      >>>
                      >>>[/color]
                      >>
                      >>I already tried this, and get this error message:
                      >>"A.h line 4, Error: Iterator is not a structure tag."[/color]
                      >
                      >
                      > The solution John gave would work for code "such as" that you've shown.
                      >
                      > How can you possibly expect anyone to help you diagnose the cause of that
                      > error message from the information you've supplied? We don't know what
                      > Iterator is, or why the compiler would possibly think it is being used as a
                      > structure tag (um, does it follow the word 'struct'?) But if you showed us
                      > line 4 (or more) of A.h, we might at least /begin/ to be able to take wild
                      > guesses...
                      > -leor
                      >[/color]
                      Iterator is class B. I forgot to modify what I 'pasted'. I greatly apologize to
                      Leor for wasting his precious time, and even more precious thought.

                      I have not used 'friends' very often, and I made the misktake of declaring B to
                      be a friend of A's. Instead of what John had correctly suggested, Making A a
                      friend of B.

                      Thanks,

                      ~S

                      Comment

                      • SaltPeter

                        #12
                        Re: access to private members of internal class


                        <snip>[color=blue][color=green]
                        > >
                        > >[/color]
                        > I already tried this, and get this error message:
                        > "A.h line 4, Error: Iterator is not a structure tag."
                        >
                        > I have tried using Sun's CC 5.0 and 5.6 beta compilers.
                        >
                        > ~S[/color]

                        The problems i see with your code are

                        a) "Class" should be replaced by "class" (probably the error you are seeing
                        by compiler)
                        b) its unclear what your goal is (just wanting code to be compileable won't
                        help)
                        c) member variables of a class should not use the underscore as first
                        character
                        d) You should specify constructors with an initializer list to pass
                        "member_" a value when constructor is invoked (so you can at least test
                        without stepping the debugger)
                        e) specify a virtual destructor as well.
                        f) show minimal code used to invoke the object(s) construction(s) and
                        behaviours.

                        You could have friendified class A as mentioned in other posts but i'ld
                        prefer doing the same to the function instead (friend A::func(...)). But if
                        class A is composed of a class B, that just doesn't make sense. add a public
                        function to class B which has access to it's own private member instead.


                        Comment

                        • SaltPeter

                          #13
                          Re: access to private members of internal class


                          <snip>[color=blue][color=green]
                          > >
                          > >[/color]
                          > I already tried this, and get this error message:
                          > "A.h line 4, Error: Iterator is not a structure tag."
                          >
                          > I have tried using Sun's CC 5.0 and 5.6 beta compilers.
                          >
                          > ~S[/color]

                          The problems i see with your code are

                          a) "Class" should be replaced by "class" (probably the error you are seeing
                          by compiler)
                          b) its unclear what your goal is (just wanting code to be compileable won't
                          help)
                          c) member variables of a class should not use the underscore as first
                          character
                          d) You should specify constructors with an initializer list to pass
                          "member_" a value when constructor is invoked (so you can at least test
                          without stepping the debugger)
                          e) specify a virtual destructor as well.
                          f) show minimal code used to invoke the object(s) construction(s) and
                          behaviours.

                          You could have friendified class A as mentioned in other posts but i'ld
                          prefer doing the same to the function instead (friend A::func(...)). But if
                          class A is composed of a class B, that just doesn't make sense. add a public
                          function to class B which has access to it's own private member instead.


                          Comment

                          • John Harrison

                            #14
                            Re: access to private members of internal class

                            > c) member variables of a class should not use the underscore as first[color=blue]
                            > character[/color]

                            Why? I do this all the time. I get moaned at on style grounds but I yet to
                            hear of concrete reason why its bad.

                            john


                            Comment

                            • John Harrison

                              #15
                              Re: access to private members of internal class

                              > c) member variables of a class should not use the underscore as first[color=blue]
                              > character[/color]

                              Why? I do this all the time. I get moaned at on style grounds but I yet to
                              hear of concrete reason why its bad.

                              john


                              Comment

                              Working...