struct in C++

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

    struct in C++

    In VC++ compiler

    struct what
    {
    int a;
    int b;
    };

    In C, an struct of type "what" is created like: struct what this;
    In C++, an object of type "what" can be instantiated like this: what this;
    Is this defined in C++ Standard or just VC++ compiler I am using
    implemented?




  • pandy.song

    #2
    Re: struct in C++

    C++ Standard defines it. Not VC++ compiler.

    "Garma" <Garmatu@nosyss erv.com> wrote in message
    news:Q_mNb.3848 8$6y6.823264@bg tnsc05-news.ops.worldn et.att.net...[color=blue]
    > In VC++ compiler
    >
    > struct what
    > {
    > int a;
    > int b;
    > };
    >
    > In C, an struct of type "what" is created like: struct what this;
    > In C++, an object of type "what" can be instantiated like this: what this;
    > Is this defined in C++ Standard or just VC++ compiler I am using
    > implemented?
    >
    >
    >
    >[/color]


    Comment

    • Andrey Tarasevich

      #3
      Re: struct in C++

      Garma wrote:[color=blue]
      > In VC++ compiler
      >
      > struct what
      > {
      > int a;
      > int b;
      > };
      >
      > In C, an struct of type "what" is created like: struct what this;
      > In C++, an object of type "what" can be instantiated like this: what this;
      > Is this defined in C++ Standard or just VC++ compiler I am using
      > implemented?[/color]

      Yes, it is defined in C++ standard. In C++ in this particular context
      (declaration) you can do it either way. Keep in mind though that in C++
      'this' is a keyword. You can't create an object named 'this'.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • E. Robert Tisdale

        #4
        Re: struct in C++

        Garma wrote:
        [color=blue]
        > In VC++ compiler
        >
        > struct what {
        > int a;
        > int b;
        > };
        >
        > In C, an [object] of type "struct what" is created like this:[/color]
        [color=blue]
        > struct what this;[/color]
        [color=blue]
        > In C++, an object of type "what" can be instantiated like this:
        >
        > what this;
        >
        > Is this defined in C++ Standard?[/color]

        No.
        'this' is a C++ keyword.
        [color=blue]
        > Or just VC++ compiler I am using implemented?[/color]

        The ANSI/ISO C standards define the C programming language.
        The ANSI/ISO C++ standards define the C++ programming language

        If you define:

        typedef struct what {
        int a;
        int b;
        } what;

        in C, you can create an object of type "struct what" like this:

        what w;

        Comment

        • Nick Hounsome

          #5
          Re: struct in C++


          "Garma" <Garmatu@nosyss erv.com> wrote in message
          news:Q_mNb.3848 8$6y6.823264@bg tnsc05-news.ops.worldn et.att.net...[color=blue]
          > In VC++ compiler
          >
          > struct what
          > {
          > int a;
          > int b;
          > };
          >
          > In C, an struct of type "what" is created like: struct what this;
          > In C++, an object of type "what" can be instantiated like this: what this;[/color]

          No it can't - 'this' is a keyword.
          [color=blue]
          > Is this defined in C++ Standard or just VC++ compiler I am using
          > implemented?
          >[/color]

          Standard - but you can write:
          struct what a;
          In both C and C++ and it is still needed in some places in C++.
          You can also get more C++ like in C by using:
          typedef struct
          {
          int a;
          int b;
          } what;
          what a;[color=blue]
          >
          >
          >[/color]


          Comment

          • Howard

            #6
            Re: struct in C++


            "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message > If
            you define:[color=blue]
            >
            > typedef struct what {
            > int a;
            > int b;
            > } what;
            >
            > in C, you can create an object of type "struct what" like this:
            >
            > what w;
            >[/color]

            You've got an extra "what" there, before the {, don't you? Should be just:

            typedef struct {
            int a;
            int b;
            } what;
            ....
            what w;

            -Howard


            Comment

            • jeffc

              #7
              Re: struct in C++


              "Garma" <Garmatu@nosyss erv.com> wrote in message
              news:Q_mNb.3848 8$6y6.823264@bg tnsc05-news.ops.worldn et.att.net...[color=blue]
              > In VC++ compiler
              >
              > struct what
              > {
              > int a;
              > int b;
              > };
              >
              > In C, an struct of type "what" is created like: struct what this;
              > In C++, an object of type "what" can be instantiated like this: what this;
              > Is this defined in C++ Standard or just VC++ compiler I am using
              > implemented?[/color]

              Standard - new "feature" of C++ (over C).


              Comment

              • E. Robert Tisdale

                #8
                Re: struct in C++

                Howard wrote:
                [color=blue]
                > E. Robert Tisdale wrote:
                >
                > If you define:
                >[color=green]
                >>typedef struct what {
                >> int a;
                >> int b;
                >> } what;
                >>
                >>in C, you can create an object of type "struct what" like this:
                >>
                >>what w;[/color]
                >
                >
                > You've got an extra "what" there, before the {, don't you? Should be just:
                >
                > typedef struct {
                > int a;
                > int b;
                > } what;
                > ...
                > what w;[/color]

                typedef struct what what;

                typedef struct other {
                what* p;
                // . . .
                };

                struct what {
                int a;
                int b;
                };

                what w;

                Comment

                • Jeff Schwab

                  #9
                  Re: struct in C++

                  Howard wrote:[color=blue]
                  > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message > If
                  > you define:
                  >[color=green]
                  >>typedef struct what {
                  >> int a;
                  >> int b;
                  >> } what;
                  >>
                  >>in C, you can create an object of type "struct what" like this:
                  >>
                  >>what w;
                  >>[/color]
                  >
                  >
                  > You've got an extra "what" there, before the {, don't you? Should be just:
                  >
                  > typedef struct {
                  > int a;
                  > int b;
                  > } what;
                  > ...
                  > what w;
                  >
                  > -Howard
                  >
                  >[/color]

                  The first "what" is the struct's tag. The second is an alternative name
                  (via typedef) for the type "struct what".

                  Comment

                  Working...