Default constructor/destructor

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

    Default constructor/destructor

    When defining a clas and no constructor and destructor provided, compiler
    generates both.

    What're the need for this since they do nothing as to
    constructing/destructing an obejct.

    What's happening in constructor/destructor if they both are defaulted and
    empty?

    Thanks!


  • cyper

    #2
    Re: Default constructor/destructor


    "ctick" <ctick@flare.co m> дÈëÏûÏ¢
    news:dMuCc.1260 02$Gx4.86970@bg tnsc04-news.ops.worldn et.att.net...[color=blue]
    > When defining a clas and no constructor and destructor provided, compiler
    > generates both.
    >
    > What're the need for this since they do nothing as to
    > constructing/destructing an obejct.[/color]
    no,if u need a stack and u define a pointer, u must alloc memory for it.
    and when to free it?
    in the destructor,test if that pointer is not NUll,and free it.


    [color=blue]
    >
    > What's happening in constructor/destructor if they both are defaulted and
    > empty?[/color]
    they will do they should do.but no more



    Comment

    • John Harrison

      #3
      Re: Default constructor/destructor


      "ctick" <ctick@flare.co m> wrote in message
      news:dMuCc.1260 02$Gx4.86970@bg tnsc04-news.ops.worldn et.att.net...[color=blue]
      > When defining a clas and no constructor and destructor provided, compiler
      > generates both.
      >
      > What're the need for this since they do nothing as to
      > constructing/destructing an obejct.[/color]

      In one object contains another object, the the constructors and destructors
      must be called for the contained objects.

      class X
      {
      X();
      };

      class Y
      {
      X x;
      };

      The generated constructor for Y will call the default constructor for X,
      because every Y contains an X. Similarly for destructors.
      [color=blue]
      >
      > What's happening in constructor/destructor if they both are defaulted and
      > empty?[/color]

      That's exactly the same as the generated ones. Default constructors and
      destructors will be called for all contained objects.
      [color=blue]
      >
      > Thanks!
      >[/color]

      john


      Comment

      • Sharad Kala

        #4
        Re: Default constructor/destructor


        "John Harrison" <john_andronicu s@hotmail.com> wrote in message
        news:2jvc2uF15m 30mU1@uni-berlin.de...[color=blue]
        >[/color]
        [snip][color=blue]
        > class X
        > {
        > X();
        > };
        >
        > class Y
        > {
        > X x;
        > };
        >
        > The generated constructor for Y will call the default constructor for X,
        > because every Y contains an X. Similarly for destructors.[/color]

        Ofcourse, you forgot to make constructor public or declare Y friend in X.


        Comment

        • cyper

          #5
          Re: Default constructor/destructor

          and when u pass an arg to a function
          the function will call the constuctor of that object automaticlly

          "ctick" <ctick@flare.co m> дÈëÏûÏ¢
          news:dMuCc.1260 02$Gx4.86970@bg tnsc04-news.ops.worldn et.att.net...[color=blue]
          > When defining a clas and no constructor and destructor provided, compiler
          > generates both.
          >
          > What're the need for this since they do nothing as to
          > constructing/destructing an obejct.
          >
          > What's happening in constructor/destructor if they both are defaulted and
          > empty?
          >
          > Thanks!
          >
          >[/color]


          Comment

          • JKop

            #6
            Re: Default constructor/destructor

            ctick posted:
            [color=blue]
            > When defining a clas and no constructor and destructor provided, compiler
            > generates both.
            >
            > What're the need for this since they do nothing as to
            > constructing/destructing an obejct.
            >
            > What's happening in constructor/destructor if they both are defaulted and
            > empty?[/color]


            Absolutely nothing:


            class SomeClass
            {
            public:

            SomeClass(void)
            {
            ;
            }

            };


            -JKop

            Comment

            • New_user

              #7
              Re: Default constructor/destructor

              "ctick" <ctick@flare.co m> wrote in message news:<dMuCc.126 002$Gx4.86970@b gtnsc04-news.ops.worldn et.att.net>...[color=blue]
              > When defining a clas and no constructor and destructor provided, compiler
              > generates both.[/color]

              Wrong! If you do not define you class constructor explicitly, compiler
              will generate constructor/destructor only in case your class have
              NON-TRIVIAL CONSTRUCTOR/DESTRUCTOR, eg:

              0. Your class has virtual member-functions - implicitly generated
              default ctor (and copy ctor) will setup vptr for your class

              1. Has members with non-trivial constructors - implicitly generated
              ctor will call them

              2. Your class has base(s) with non-trivial ctor(s) - the same.

              etc.

              Comment

              • SaltPeter

                #8
                Re: Default constructor/destructor


                "ctick" <ctick@flare.co m> wrote in message
                news:dMuCc.1260 02$Gx4.86970@bg tnsc04-news.ops.worldn et.att.net...[color=blue]
                > When defining a clas and no constructor and destructor provided, compiler
                > generates both.
                >
                > What're the need for this since they do nothing as to
                > constructing/destructing an obejct.
                >
                > What's happening in constructor/destructor if they both are defaulted and
                > empty?
                >
                > Thanks!
                >
                >[/color]

                The answer is that without a constructor and destructor, compiler generated
                or not, you can't create and/or destroy an instance of a class. Don't be
                fooled by other languages that don't expose the existance of cstors and
                d~stors. They certainly need them as well. The difference here is that you
                have the option to define how your instance is initialized / constructed and
                destroyed.

                While the compiler generated constructor might fit the bill, you always have
                the option of taking control in the case the default constructor doesn't
                fullfill your needs. Consider:

                class A
                {
                int m_number; // private variable
                public:
                A(n) : m_number(n) { } // cstor
                ~A() { } // d~stor
                };

                Note that i can now keep the m_number variable encapsulated without the need
                to provide a member function to initialize it. The private variable is
                initialized when the cstor is invoked with a default of 5 unless otherwise
                specified.

                #include <iostream>

                class A
                {
                int m_number;
                public:
                A(int n = 5) : m_number(n) { }
                ~A() { }
                void display() const { std::cout << "number is " << m_number <<
                std::endl; }
                };

                int main()
                {
                A a;
                a.display();

                A aa(10);
                aa.display();

                return 0;
                }

                number is 5
                number is 10



                Comment

                • Robbie Hatley

                  #9
                  Re: Default constructor/destructor

                  "cyper" <amethyst_sun@s ohu.com> wrote:
                  [color=blue]
                  > when u pass an arg to a function the function will call
                  > the constuctor of that object automaticlly[/color]

                  Not necessarily. Depends on how you pass the arg:
                  by value, by reference, or by pointer:

                  #include <iostream>

                  struct splat {char broiled;};

                  void Func1(splat par) {std::cout << par.broiled << std::endl;}
                  void Func2(const splat& par) {std::cout << par.broiled << std::endl;}
                  void Func3(splat* par) {std::cout << par->broiled << std::endl;}

                  int main(void)
                  {
                  splat blat; // Calls splat's implicit default constructor
                  blat.broiled = 'a';
                  Func1(blat); // Calls splat's implicit copy constructor
                  Func2(blat); // Does NOT call any constructors
                  Func3(&blat); // Does NOT call any constructors
                  return 0;
                  }

                  --
                  Cheers,
                  Robbie Hatley
                  Tustin, CA, USA
                  email: lonewolfintj at pacbell dot net
                  web: home dot pacbell dot net slant earnur slant






                  ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
                  http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
                  ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

                  Comment

                  Working...