J
Juha Nieminen
This is more a theoretical question, so please skip on nitpicking on
whether it "makes sense".
Suppose that I have a class named eg. Counter which constructor takes
an integer as parameter. I can do things like this without problems:
for(Counter c = 5; ...)
for(Counter c(5); ...)
if(Counter c = 5) ...
However, this will produce a compiler error, at least with gcc:
if(Counter c(5)) ...
The error message I'm getting is: "error: expected primary-expression
before 'c'"
There is a definite difference between "Counter c = 5" and
"Counter c(5)": The former requires for the copy constructor to be
accessible. If it isn't, then the former syntax cannot be used.
In that case Counter instances can be created with the for() statement
by using "for(Counter c(5); ...)". However, I can't think of a way of
doing the same with the if() statement.
Why is there such a difference? Would it be possible to create such
instances within an if() statement?
One could think that creating an object inside the condition of an
if() statement makes absolutely no sense, but there might be situations
where it could make at least some sense, for example:
if(InputStream iStream("file.txt")) doSomethingWith(iStream);
else log("Could not open file.txt");
Note that the above works fine if InputStream is copyable, by using
the other syntax, ie:
if(InputStream iStream = "file.txt") doSomethingWith(iStream);
else log("Could not open file.txt");
However, if it's not copyable, then I don't know how it could be done.
(And no, I'm not asking about alternative ways of doing the same thing.
I know C++ well enough to figure out on my own. It's not what I'm asking.)
whether it "makes sense".
Suppose that I have a class named eg. Counter which constructor takes
an integer as parameter. I can do things like this without problems:
for(Counter c = 5; ...)
for(Counter c(5); ...)
if(Counter c = 5) ...
However, this will produce a compiler error, at least with gcc:
if(Counter c(5)) ...
The error message I'm getting is: "error: expected primary-expression
before 'c'"
There is a definite difference between "Counter c = 5" and
"Counter c(5)": The former requires for the copy constructor to be
accessible. If it isn't, then the former syntax cannot be used.
In that case Counter instances can be created with the for() statement
by using "for(Counter c(5); ...)". However, I can't think of a way of
doing the same with the if() statement.
Why is there such a difference? Would it be possible to create such
instances within an if() statement?
One could think that creating an object inside the condition of an
if() statement makes absolutely no sense, but there might be situations
where it could make at least some sense, for example:
if(InputStream iStream("file.txt")) doSomethingWith(iStream);
else log("Could not open file.txt");
Note that the above works fine if InputStream is copyable, by using
the other syntax, ie:
if(InputStream iStream = "file.txt") doSomethingWith(iStream);
else log("Could not open file.txt");
However, if it's not copyable, then I don't know how it could be done.
(And no, I'm not asking about alternative ways of doing the same thing.
I know C++ well enough to figure out on my own. It's not what I'm asking.)