default ctor

S

subramanian100in

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

dave_mikesell

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) {}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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) { }
 
R

Ron Natalie

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.
 
R

Ron Natalie

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.
 
D

dave_mikesell

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top