473,569 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c'tor not called?

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

Jul 23 '05 #1
9 1034
Sashi wrote:
A a();


Compare that with:

int f();

It's a quirky corner of the syntax. Get rid of the parentheses in the
original.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

"Sashi" <sm******@gmail .com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
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
Jul 23 '05 #3
Mike Wahler wrote:
"Sashi" <sm******@gmail .com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
#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
Jul 23 '05 #4
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.

Jul 23 '05 #5
On 2005-06-27 22:24:35 -0400, "jf*******@gmai l.com" <jf*******@gmai l.com> said:
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.


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 */ }
}
--
Clark S. Cox, III
cl*******@gmail .com

Jul 23 '05 #6
I thought default was when you didnt specify anything.
Now i dont understand whats ambigous in there. . .

Jul 23 '05 #7
* jf*******@gmail .com:
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).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #8
Sashi wrote:
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-lit....html#faq-10.2

Jul 23 '05 #9

<jf*******@gmai l.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
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
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1198
by: Jakob Bieling | last post by:
Hi, Recently, I have seen code where the assignment operator has a return type 'void'. I do understand that it is a 'bad idea' in the sense that I am limiting myself, when using that operator= (ie. v = w = x would not be possible). But I asked myself if this is legal. Even though I would not know why this should be illegal (Standard did not...
7
21283
by: Privacy Advocate | last post by:
//crossposted to: comp.lang.javascript, alt.comp.lang.javascript in an effort to get factual answers from JavaScript experts// Simply put; Is it possible to obtain the real (actual) IP address of someone (client) that visits a web site through an anonymous proxy if this person ONLY has JavaScript enabled in their browser? This is NOT a...
8
1324
by: Protoman | last post by:
#include <iostream> #include <cstdlib> #include <cmath> #include "SmrtPtr.hpp" using namespace std; struct Data { double plus; double mult;
4
1535
by: Tony Johansson | last post by:
Hello! I have a class definition called MyClass see below. I create an instance of this class MyClass I also want this instance to be able to modify the test instance that exist in this class. I can make the instance of this class MyClass to be able to access the instance test in two ways.
6
2143
by: tony | last post by:
Hello! When exactly is it important or advisable to use this form load event handler compare to using the C-tor. For example here I create an event handler called dataBoundGridForm that is called when the form is loaded. this.Load += new System.EventHandler(this.dataBoundGridForm_Load); I mean if I compare form load with the C-tor I...
15
2382
by: Victor Bazarov | last post by:
Hello, Take a look at this program: ----------------------------------- class B { B(const B&); B& operator=(const B&); public: B(int);
2
1716
by: Zytan | last post by:
When you create a Windows app, you get a two files pre-made each with a partial class of the same class, and you also get a c'tor of your class which calls InitializeComponent(). I guess this is cool, since VB hides this, I believe. I am sure it is ok for me to populate the c'tor with more of my own stuff, as long as I don't remove the...
3
2256
by: RainBow | last post by:
I understand that a compiler synthesises a default constructor if none is provided by the user ( of course depending on the situation if synthesis of such c'tor is actually needed in the program e.g if vptr is needed, default c'tor must be synthesised by compiler). In the class Temporary below, there is no reason why a compiler should...
4
1596
by: TonyJ | last post by:
Hello! Assume I have the two classes Test1 and Test2. They are very similar. Test1 initialize the instance variable in the C-tor but Test2 initialize the instance variable without using the C-tor. Both classes can be seen below. In this simple example it doesn't matter because Test1 is the same as Test2. Do you agree with me.
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.