Default class members generated by C++

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

    Default class members generated by C++

    Hi,
    As per the C++ standard, after the compilation of the following class
    "Test" how many / which members would be generated by the compiler.
    and actually I am also interested behind the logic doing so by C++ for
    this empty class "Test" which is not even utilized in the program.

    #include <iostream>
    using namespace std;

    class Test
    {
    };

    void main()
    {
    }

    Many Thanks

  • Daniel T.

    #2
    Re: Default class members generated by C++

    "BeautifulM ind" <msjammu@gmail. comwrote:
    As per the C++ standard, after the compilation of the following class
    "Test" how many / which members would be generated by the compiler.
    and actually I am also interested behind the logic doing so by C++ for
    this empty class "Test" which is not even utilized in the program.
    >
    #include <iostream>
    using namespace std;
    >
    class Test
    {
    };
    >
    void main()
    {
    }
    >
    Many Thanks
    I don't think any members are *required* to be generated. The "as if"
    rule kicks in and since Test isn't used, the compiler need to nothing
    with it.

    Comment

    • Neo

      #3
      Re: Default class members generated by C++



      On Jan 29, 4:16 pm, "BeautifulM ind" <msja...@gmail. comwrote:
      Hi,
      As per the C++ standard, after the compilation of the following class
      "Test" how many / which members would be generated by the compiler.
      and actually I am also interested behind the logic doing so by C++ for
      this empty class "Test" which is not even utilized in the program.
      >
      #include <iostream>
      using namespace std;
      >
      class Test
      {
      >
      };
      >void main()
      {
      >
      }Many Thanks
      Hi,
      For above mentioned code nothing will be generated as you have not
      used Test class anywhere.
      but if the code is

      void main()
      {
      Test obj;
      }

      Only one bye of will be added as hidden data member. This is to
      differentiate among the other instances of same class. default ctor/
      dtor, copy ctor and assignment operator will not get generated coz
      there is no any need.

      Regards
      Vikram S



      Comment

      • BeautifulMind

        #4
        Re: Default class members generated by C++

        Hi thanks for the reply,
        >For above mentioned code nothing will be generated as you have not
        >used Test class anywhere.
        class Test
        {
        };
        void main()
        {
        }

        So this means that given the above code, the compiler will remove the
        Test class altogether from the object code (which it should I guess)
        becuase it is not unilized anywhere?

        Regards,
        BeautifulMind


        Comment

        • Ian Collins

          #5
          Re: Default class members generated by C++

          BeautifulMind wrote:
          Hi thanks for the reply,
          >
          >
          >>For above mentioned code nothing will be generated as you have not
          >>used Test class anywhere.
          >
          >
          class Test
          {
          };
          void main()
          {
          }
          >
          So this means that given the above code, the compiler will remove the
          Test class altogether from the object code (which it should I guess)
          becuase it is not unilized anywhere?
          >
          unilized? Yes and no, the class is empty, so there isn't anything to
          remove.

          The compiler will get upset with you for writing "void main()".

          --
          Ian Collins.

          Comment

          • Daniel T.

            #6
            Re: Default class members generated by C++

            "BeautifulM ind" <msjammu@gmail. comwrote:
            Hi thanks for the reply,
            >
            For above mentioned code nothing will be generated as you have not
            used Test class anywhere.
            >
            class Test
            {
            };
            void main()
            {
            }
            >
            So this means that given the above code, the compiler will remove the
            Test class altogether from the object code (which it should I guess)
            becuase it is not unilized anywhere?
            As an absolute? There is no telling. That's a quality of implementation
            issue.

            The compiler certainly _can_ remove the class, and I think it is safe to
            say that, given the correct options, most _do_ remove the class, but
            there is no guarantee that all compilers _will_ remove it.

            Comment

            Working...