default ctor

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Apr 16, 2007.

  1. , India

    , India Guest

    Consider the program

    #include <iostream>
    #include <string>

    class A {
    public:
    void print(void) const;
    A(const std::string &s) : str(s) { }

    private:
    std::string str;
    };

    void A::print(void) const
    {
    std::cout << "A::print() - " << str << '\n';

    return;
    }

    int main(void)
    {
    A obj;

    obj.print();

    return 0;
    }

    If I compile this program under Linux, I am getting the compilation
    error
    error: no matching function for call to `A::A()'

    for the line
    A obj;

    I thought the compiler will provide the default constructor A::A().
    But it doesn't seem to. Kindly explain the reason.

    If I remove my constructor
    A(const std::string &s) : str(s) { }
    then it compiles well and the compiler provides the default
    constructor. Why is this difference ?
    , India, Apr 16, 2007
    #1
    1. Advertising

  2. , India

    Guest

    On Apr 16, 8:08 am, ", India"
    <> wrote:

    > I thought the compiler will provide the default constructor A::A().
    > But it doesn't seem to. Kindly explain the reason.
    >
    > If I remove my constructor
    > A(const std::string &s) : str(s) { }
    > then it compiles well and the compiler provides the default
    > constructor. Why is this difference ?


    If you define a constructor that accepts arguments, then the compiler
    will not generate a default constructor for you. Think about the
    reason it generates one when you don't define one.

    Back to your code...what would the behavior of A() be? If it is just
    to initialize str to "", you can accomplish that be defaulting the
    parameter in your other constructor:

    A(const std::string & s = "") : str(s) {}
    , Apr 16, 2007
    #2
    1. Advertising

  3. On 16 Apr, 14:08, ", India"
    <> wrote:
    > Consider the program
    >
    > #include <iostream>
    > #include <string>
    >
    > class A {
    > public:
    > void print(void) const;
    > A(const std::string &s) : str(s) { }
    >
    > private:
    > std::string str;
    >
    > };
    >
    > void A::print(void) const
    > {
    > std::cout << "A::print() - " << str << '\n';
    >
    > return;
    >
    > }
    >
    > int main(void)
    > {
    > A obj;
    >
    > obj.print();
    >
    > return 0;
    >
    > }
    >
    > If I compile this program under Linux, I am getting the compilation
    > error
    > error: no matching function for call to `A::A()'
    >
    > for the line
    > A obj;
    >
    > I thought the compiler will provide the default constructor A::A().
    > But it doesn't seem to. Kindly explain the reason.
    >
    > If I remove my constructor
    > A(const std::string &s) : str(s) { }
    > then it compiles well and the compiler provides the default
    > constructor. Why is this difference ?


    It will provide a default constructor as long as you don't provide a
    constructor. After all, if it always made one how would you prevent it
    from creating one when you don't want one? Notice that you can convert
    your constructor to a default constructor by adding a default-value to
    the string:

    A(const std::string& s = "") : str(s) { }

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Apr 16, 2007
    #3
  4. , India

    Ron Natalie Guest

    , India wrote:

    >
    > I thought the compiler will provide the default constructor A::A().
    > But it doesn't seem to. Kindly explain the reason.
    >
    > If I remove my constructor
    > A(const std::string &s) : str(s) { }
    > then it compiles well and the compiler provides the default
    > constructor. Why is this difference ?
    >

    If you define ANY constructors, the compiler doesn't provide a
    default constructor for you. That's how it knows that you
    don't want a default constructor.

    I'm not sure what the whole point of your copy constructor is.
    It is identical to the one the compiler would have generated
    anyhow (and it's a good thing since you don't define a copy
    assignment operator which would have been necessary to fully
    implement copy semantics if they were different from the default).

    That's the joy of using members that have proper object
    behavior. You don't have to define default initializers,
    copy, or destruction code for them specifically, nor for
    objects that contain them.
    Ron Natalie, Apr 16, 2007
    #4
  5. , India

    Ron Natalie Guest

    wrote:

    > If you define a constructor that accepts arguments, then the compiler
    > will not generate a default constructor for you. Think about the
    > reason it generates one when you don't define one.
    >

    If you define ANY constructor, the compiler does not define one for you.
    Ron Natalie, Apr 16, 2007
    #5
  6. , India

    Guest

    On Apr 16, 12:48 pm, Ron Natalie <> wrote:
    > wrote:
    > > If you define a constructor that accepts arguments, then the compiler
    > > will not generate a default constructor for you. Think about the
    > > reason it generates one when you don't define one.

    >
    > If you define ANY constructor, the compiler does not define one for you.


    Right. I guess I could have added that if you define a constructor
    with no arguments, then the compiler would not also define a
    constructor with no arguments.
    , Apr 16, 2007
    #6
    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. Apricot
    Replies:
    4
    Views:
    511
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    477
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    464
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    399
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. puzzlecracker
    Replies:
    8
    Views:
    417
    James Kanze
    Apr 15, 2008
Loading...

Share This Page