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