protected and private in C++ and C#

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

    protected and private in C++ and C#

    1st, simple question, is there a protected in C#?

    I mean a comparable keyword like the C++ protected, which defines a member
    as accessable for decendants, but not for the outside world.

    Then, a more complicated question, is privat in C#, then comparable with
    protected, or private in C++. Or to say it in other words, if I declare a
    member private in C#, can I access it in a decendant class?


  • Stefan Simek

    #2
    Re: protected and private in C++ and C#

    My4thPersonalit y wrote:[color=blue]
    > 1st, simple question, is there a protected in C#?
    >
    > I mean a comparable keyword like the C++ protected, which defines a member
    > as accessable for decendants, but not for the outside world.
    >
    > Then, a more complicated question, is privat in C#, then comparable with
    > protected, or private in C++. Or to say it in other words, if I declare a
    > member private in C#, can I access it in a decendant class?
    >
    >[/color]
    The protected and private in C# have the same meaning as in C++. C#
    extends access modifiers by adding internal and protected internal,
    which are equal to public and protected with respect to the module they
    appear in and private to the outer world.

    Comment

    • Stefan Simek

      #3
      Re: protected and private in C++ and C#

      My4thPersonalit y wrote:[color=blue]
      > 1st, simple question, is there a protected in C#?
      >
      > I mean a comparable keyword like the C++ protected, which defines a member
      > as accessable for decendants, but not for the outside world.
      >
      > Then, a more complicated question, is privat in C#, then comparable with
      > protected, or private in C++. Or to say it in other words, if I declare a
      > member private in C#, can I access it in a decendant class?
      >
      >[/color]
      The protected and private in C# have the same meaning as in C++. C#
      extends access modifiers by adding internal and protected internal,
      which are equal to public in the module in which they appear and private
      or protected respectively to the outer world.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: protected and private in C++ and C#

        Stefan Simek <simek.nospam@t riaxis.nospam.s k> wrote:[color=blue][color=green]
        > > Then, a more complicated question, is privat in C#, then comparable with
        > > protected, or private in C++. Or to say it in other words, if I declare a
        > > member private in C#, can I access it in a decendant class?[/color][/color]
        [color=blue]
        > The protected and private in C# have the same meaning as in C++.[/color]

        Is that strictly true? I *thought* that in C++, you couldn't access the
        private members of one instance from another instance (whereas you can
        in C#). This is only based on what others have said, however - I
        haven't checked the spec.

        There's also the concept of private inheritance in C++, but not in C#.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • Andreas Mueller

          #5
          Re: protected and private in C++ and C#

          Jon Skeet [C# MVP] wrote:[color=blue]
          > Stefan Simek <simek.nospam@t riaxis.nospam.s k> wrote:
          >[color=green][color=darkred]
          >>>Then, a more complicated question, is privat in C#, then comparable with
          >>>protected, or private in C++. Or to say it in other words, if I declare a
          >>>member private in C#, can I access it in a decendant class?[/color][/color]
          >
          >[color=green]
          >>The protected and private in C# have the same meaning as in C++.[/color]
          >
          >
          > Is that strictly true? I *thought* that in C++, you couldn't access the
          > private members of one instance from another instance (whereas you can
          > in C#). This is only based on what others have said, however - I
          > haven't checked the spec.[/color]
          Yes, you can access the private fields from another instance in C++.[color=blue]
          >
          > There's also the concept of private inheritance in C++, but not in C#.[/color]


          HTH,
          Andy
          --
          To email me directly, please remove the *NO*SPAM* parts below:
          *NO*SPAM*xmen40 @*NO*SPAM*gmx.n et

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: protected and private in C++ and C#

            Andreas Mueller <me@privacy.net > wrote:[color=blue][color=green]
            > > Is that strictly true? I *thought* that in C++, you couldn't access the
            > > private members of one instance from another instance (whereas you can
            > > in C#). This is only based on what others have said, however - I
            > > haven't checked the spec.[/color][/color]
            [color=blue]
            > Yes, you can access the private fields from another instance in C++.[/color]

            My mistake. It must be another language I'm thinking of... I wonder
            which?

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • Mike Schilling

              #7
              Re: protected and private in C++ and C#


              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1e221c 7e7812760598cc3 f@msnews.micros oft.com...[color=blue]
              > Andreas Mueller <me@privacy.net > wrote:[color=green][color=darkred]
              >> > Is that strictly true? I *thought* that in C++, you couldn't access the
              >> > private members of one instance from another instance (whereas you can
              >> > in C#). This is only based on what others have said, however - I
              >> > haven't checked the spec.[/color][/color]
              >[color=green]
              >> Yes, you can access the private fields from another instance in C++.[/color]
              >
              > My mistake. It must be another language I'm thinking of... I wonder
              > which?[/color]

              None I know of. In fact, "How come instance 1 can access private members of
              instance 2?" seems to be a common newbie question about C#, C++, and Java
              alike.


              Comment

              • David Anton

                #8
                Re: protected and private in C++ and C#

                Actually, C++/CLI has "internal" and also has an equivalent to C#'s protected
                internal: "public protected".
                --
                David Anton
                Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

                Instant C#: VB to C# converter
                Instant VB: C# to VB converter
                Instant C++: C# to C++ converter & VB to C++ converter
                Instant J#: VB to J# converter



                "Stefan Simek" wrote:
                [color=blue]
                > My4thPersonalit y wrote:[color=green]
                > > 1st, simple question, is there a protected in C#?
                > >
                > > I mean a comparable keyword like the C++ protected, which defines a member
                > > as accessable for decendants, but not for the outside world.
                > >
                > > Then, a more complicated question, is privat in C#, then comparable with
                > > protected, or private in C++. Or to say it in other words, if I declare a
                > > member private in C#, can I access it in a decendant class?
                > >
                > >[/color]
                > The protected and private in C# have the same meaning as in C++. C#
                > extends access modifiers by adding internal and protected internal,
                > which are equal to public in the module in which they appear and private
                > or protected respectively to the outer world.
                >[/color]

                Comment

                Working...