Default constructor/destructor

Discussion in 'C++' started by ctick, Jun 24, 2004.

  1. ctick

    ctick Guest

    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!
     
    ctick, Jun 24, 2004
    #1
    1. Advertising

  2. "ctick" <> wrote in message
    news:dMuCc.126002$...
    > 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.


    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.

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


    That's exactly the same as the generated ones. Default constructors and
    destructors will be called for all contained objects.

    >
    > Thanks!
    >


    john
     
    John Harrison, Jun 24, 2004
    #2
    1. Advertising

  3. ctick

    cyper Guest

    "ctick" <> дÈëÏûÏ¢
    news:dMuCc.126002$...
    > 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.

    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.



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

    they will do they should do.but no more
     
    cyper, Jun 24, 2004
    #3
  4. ctick

    Sharad Kala Guest

    "John Harrison" <> wrote in message
    news:...
    >

    [snip]
    > 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.


    Ofcourse, you forgot to make constructor public or declare Y friend in X.
     
    Sharad Kala, Jun 24, 2004
    #4
  5. ctick

    cyper Guest

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

    "ctick" <> дÈëÏûÏ¢
    news:dMuCc.126002$...
    > 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, Jun 24, 2004
    #5
  6. ctick

    JKop Guest

    ctick posted:

    > 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?



    Absolutely nothing:


    class SomeClass
    {
    public:

    SomeClass(void)
    {
    ;
    }

    };


    -JKop
     
    JKop, Jun 24, 2004
    #6
  7. ctick

    New_user Guest

    "ctick" <> wrote in message news:<dMuCc.126002$>...
    > When defining a clas and no constructor and destructor provided, compiler
    > generates both.


    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.
     
    New_user, Jun 24, 2004
    #7
  8. ctick

    SaltPeter Guest

    "ctick" <> wrote in message
    news:dMuCc.126002$...
    > 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!
    >
    >


    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
     
    SaltPeter, Jun 24, 2004
    #8
  9. "cyper" <> wrote:

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


    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 =---
     
    Robbie Hatley, Jun 28, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frs
    Replies:
    20
    Views:
    756
    Alf P. Steinbach
    Sep 21, 2005
  2. arun
    Replies:
    2
    Views:
    547
    benben
    Jun 13, 2006
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,244
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    421
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,170
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page