Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > default ctor

Reply
Thread Tools

default ctor

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      04-16-2007
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:rint(void) const
{
std::cout << "A:rint() - " << 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 ?

 
Reply With Quote
 
 
 
 
dave_mikesell@fastmail.fm
Guest
Posts: n/a
 
      04-16-2007
On Apr 16, 8:08 am, "(E-Mail Removed), India"
<(E-Mail Removed)> 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) {}

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      04-16-2007
On 16 Apr, 14:08, "(E-Mail Removed), India"
<(E-Mail Removed)> 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:rint(void) const
> {
> std::cout << "A:rint() - " << 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

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      04-16-2007
http://www.velocityreviews.com/forums/(E-Mail Removed), 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.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      04-16-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
dave_mikesell@fastmail.fm
Guest
Posts: n/a
 
      04-16-2007
On Apr 16, 12:48 pm, Ron Natalie <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments