how to call default ctor

M

Mark

So, I was looking at this code example by Bruce Eckel where he hides
the copy constructor by making it private... but in his code I noticed
that he writes

NoCC n();

*with* parentheses. Now I've learnt that this does *not* actually call
the default constructor (which leads me to the main question later
on..) ..but then, what exactly is n? Seems like an uninitialized
reference to NoCC...

Now my main question is.. I tried adding a default constructor to see
if it would be called (see below)..which it isn't..so I removed the ()
and now my compiler complains about an ambiguous constructor call...so
how exactly do I make this program use the constructor with no
arguments?


#include <iostream>
using namespace std;
class NoCC {
int i;
NoCC(const NoCC&); // No definition
public:
NoCC() { cout << "default ctor"; }
NoCC(int ii = 0) : i(ii) { cout << "int ctor";}
};

void f(NoCC);

int main() {
NoCC n;

cout << endl;
system("PAUSE");
}



On second thought, you'll probably just tell me that if the int
parameter is optional, there's no sense having a second constructor
that takes no arguments. Although I'm still curious if it's
possible... so I'll post this anyway.
 
V

Victor Bazarov

Mark said:
So, I was looking at this code example by Bruce Eckel where he hides
the copy constructor by making it private... but in his code I noticed
that he writes

NoCC n();

*with* parentheses. Now I've learnt that this does *not* actually
call the default constructor (which leads me to the main question
later on..) ..but then, what exactly is n? Seems like an
uninitialized reference to NoCC...

It's a function that takes no arguments and returns a 'NoCC'.
Now my main question is.. I tried adding a default constructor to see
if it would be called (see below)..which it isn't..so I removed the ()
and now my compiler complains about an ambiguous constructor call...so
how exactly do I make this program use the constructor with no
arguments?

Remove the extraneous constructor from the class.
#include <iostream>
using namespace std;
class NoCC {
int i;
NoCC(const NoCC&); // No definition
public:
NoCC() { cout << "default ctor"; }
NoCC(int ii = 0) : i(ii) { cout << "int ctor";}

This is not "int ctor". It's *also* a default constructor, which
you cannot have more than one in your class.
};

void f(NoCC);

int main() {
NoCC n;

cout << endl;
system("PAUSE");
}



On second thought, you'll probably just tell me that if the int
parameter is optional, there's no sense having a second constructor
that takes no arguments. Although I'm still curious if it's
possible... so I'll post this anyway.

No, it's not possible. There can be only one default constructor.

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

thanks for the reply, but..if it returns a NoCC..how is it initialized?

Which? The NoCC that it returns? That's up to the definition. What
Victor meant was that typing 'NoCC n();' declares a function n taking no
arguments and returning an NoCC.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top