Default constuctor

Discussion in 'C++' started by Tapeesh, Mar 1, 2005.

  1. Tapeesh

    Tapeesh Guest

    Does C++ say that a default constructor needs to be generated by the
    compiler even if a class has the default constructor defined ? Say for
    eg. there is a class

    class A
    {

    int i;
    A()
    {
    i=0;
    }
    };


    class B
    {
    int i;
    void foo()
    {
    i=0;
    }
    };

    In this case will the compiler generate its own default constructor for
    class A like in the case of class B and also create the one defined
    in the class ? If yes, then what is the functionality of this compiler
    generated constructor.
     
    Tapeesh, Mar 1, 2005
    #1
    1. Advertising

  2. "Tapeesh" <> wrote in message
    news:...
    > Does C++ say that a default constructor needs to be generated by the
    > compiler even if a class has the default constructor defined ? Say for
    > eg. there is a class

    <<snip>>
    No. Once you supply a consstructor the compiler will not provide one.
    --
    Gary
     
    Gary Labowitz, Mar 1, 2005
    #2
    1. Advertising

  3. Tapeesh

    John Carson Guest

    "Tapeesh" <> wrote in message
    news:
    > Does C++ say that a default constructor needs to be generated by the
    > compiler even if a class has the default constructor defined ? Say for
    > eg. there is a class
    >
    > class A
    > {
    >
    > int i;
    > A()
    > {
    > i=0;
    > }
    > };
    >
    >
    > class B
    > {
    > int i;
    > void foo()
    > {
    > i=0;
    > }
    > };
    >
    > In this case will the compiler generate its own default constructor
    > for class A like in the case of class B and also create the one
    > defined in the class ? If yes, then what is the functionality of this
    > compiler generated constructor.


    The compiler will NOT generate a default constructor if you define ANY
    constructor for a class. Consider for example:

    class A
    {
    int i;
    public:
    A(int n)
    {
    i=n;
    }
    };

    This doesn't have a default constructor. Thus if you write:

    int main()
    {
    A a;
    }

    then it won't compile because

    A a;

    requires a default constructor and class A does not have one. If you change
    it to, say,

    A a(5);

    then it compiles without a problem.

    Incidentally, your original class declaration:

    class A
    {
    int i;
    A()
    {
    i=0;
    }
    };

    has a private constructor, so

    A a;

    won't compile. You need to make the constructor public.

    --
    John Carson
     
    John Carson, Mar 1, 2005
    #3
  4. Tapeesh

    Tapeesh Guest

    John Carson wrote:
    > "Tapeesh" <> wrote in message
    > news:
    > > Does C++ say that a default constructor needs to be generated by

    the
    > > compiler even if a class has the default constructor defined ? Say

    for
    > > eg. there is a class
    > >
    > > class A
    > > {
    > >
    > > int i;
    > > A()
    > > {
    > > i=0;
    > > }
    > > };
    > >
    > >
    > > class B
    > > {
    > > int i;
    > > void foo()
    > > {
    > > i=0;
    > > }
    > > };
    > >
    > > In this case will the compiler generate its own default constructor
    > > for class A like in the case of class B and also create the one
    > > defined in the class ? If yes, then what is the functionality of

    this
    > > compiler generated constructor.

    >
    > The compiler will NOT generate a default constructor if you define

    ANY
    > constructor for a class. Consider for example:
    >
    > class A
    > {
    > int i;
    > public:
    > A(int n)
    > {
    > i=n;
    > }
    > };
    >
    > This doesn't have a default constructor. Thus if you write:
    >
    > int main()
    > {
    > A a;
    > }
    >
    > then it won't compile because
    >
    > A a;
    >
    > requires a default constructor and class A does not have one. If you

    change
    > it to, say,
    >
    > A a(5);
    >
    > then it compiles without a problem.
    >
    > Incidentally, your original class declaration:
    >
    > class A
    > {
    > int i;
    > A()
    > {
    > i=0;
    > }
    > };
    >
    > has a private constructor, so
    >
    > A a;
    >
    > won't compile. You need to make the constructor public.
    >
    > --
    > John Carson



    What i wanted to ask is that when the compiler compiles a code at that
    time while creating an object model for the source code, does the
    compiler generate some default constructor even if some constructor
    (default or otherwise) has been defined for the class?
     
    Tapeesh, Mar 2, 2005
    #4
  5. Tapeesh

    John Carson Guest

    "Tapeesh" <> wrote in message
    news:
    >
    > What i wanted to ask is that when the compiler compiles a code at that
    > time while creating an object model for the source code, does the
    > compiler generate some default constructor even if some constructor
    > (default or otherwise) has been defined for the class?


    The answer has already been given twice. NO.


    --
    John Carson
     
    John Carson, Mar 2, 2005
    #5
    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. micky

    default.mspx or default.do

    micky, Dec 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    498
    Andrew Robinson
    Dec 17, 2005
  2. Alexander Stippler
    Replies:
    3
    Views:
    440
    Leor Zolman
    Apr 9, 2004
  3. Replies:
    18
    Views:
    509
    Steve Pope
    Oct 5, 2006
  4. Sid K
    Replies:
    2
    Views:
    269
    Sid K
    Aug 7, 2008
  5. Unknown Poster

    still crabby about copy constuctor craziness

    Unknown Poster, Jul 24, 2004, in forum: Perl Misc
    Replies:
    16
    Views:
    201
    Ilya Zakharevich
    Aug 2, 2004
Loading...

Share This Page