c'tor not called?

S

Sashi

Hi, when I run the following piece, I don't get any output, suggesting
that the default c'tor is not being called. Why?

#include <iostream>

class A{
public:
A(){std::cout<<"Null \n";};
A(int x = 2, int y = 4){std::cout<<"Not Null \n";};
};

int main(){
A a();

}
Thanks,
Sashi
 
M

Mike Wahler

Sashi said:
Hi, when I run the following piece, I don't get any output, suggesting
that the default c'tor is not being called. Why?

Because you did not create any objects.
#include <iostream>

class A{
public:
A(){std::cout<<"Null \n";};
A(int x = 2, int y = 4){std::cout<<"Not Null \n";};
};

int main(){
A a();

This does not create an object. It declares
a function named 'a' which takes no arguments
and returns a type 'A' value.

Create a type 'A' object:

A a;

Only use parentheses if supplying arguments, e.g.

A a(0, 0);

Also note that above you have defined two default
constructors. I'm not sure what the standard has
to say about that, but it 'smells bad' to me. :)

-Mike
 
K

Kristo

Mike said:
Sashi said:
#include <iostream>

class A{
public:
A(){std::cout<<"Null \n";};
A(int x = 2, int y = 4){std::cout<<"Not Null \n";};
};

int main(){
A a();
[snip]
Also note that above you have defined two default
constructors. I'm not sure what the standard has
to say about that, but it 'smells bad' to me. :)

The compiler will complain about an ambiguity if no arguments are given
when constructing an A object.

Kristo
 
J

jfroussel

Also note that above you have defined two default
constructors. I'm not sure what the standard has
to say about that, but it 'smells bad' to me. :)

the standard explicitely support multiple Ctors. It's called
overloading.
And the second one isnt "default", since its got parameters.

i think.
 
C

Clark S. Cox III

the standard explicitely support multiple Ctors. It's called
overloading.

But they have default values, that overload is ambiguous.
And the second one isnt "default", since its got parameters.

Yes it is, because of the defaults provided.

struct Foo
{
Foo(int i=1234) { /* This is the default constructor */ }
}
 
J

jfroussel

I thought default was when you didnt specify anything.
Now i dont understand whats ambigous in there. . .
 
A

Alf P. Steinbach

* (e-mail address removed):
I thought default was when you didnt specify anything.
Now i dont understand whats ambigous in there. . .

A default constructor is one that can be called without arguments,
and both constructors can (and that's ambigous wrt. to the call).
 
J

Jaspreet

Sashi said:
Hi, when I run the following piece, I don't get any output, suggesting
that the default c'tor is not being called. Why?

#include <iostream>

class A{
public:
A(){std::cout<<"Null \n";};
A(int x = 2, int y = 4){std::cout<<"Not Null \n";};
};

int main(){
A a();

}
Thanks,
Sashi

You are not creating an object. You are actaully declaring a function a
that returns A.
Look at:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2
 
M

Mike Wahler

Also note that above you have defined two default
constructors. I'm not sure what the standard has
to say about that, but it 'smells bad' to me. :)

the standard explicitely support multiple Ctors. It's called
overloading.

Right. But there's no way to distinguish the two that
the OP wrote. I.e. the compiler cannot resolve the overloaded
name (ambiguity)
And the second one isnt "default", since its got parameters.

Yes, it is a default ctor. It's not absence of parameters
that causes a ctor to be default, it's whether it can be
called without any. A ctor with all defaulted arguments
qualifies as a default ctor.

-Mike
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top