 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
dayton Guest
|
Posted: Fri Jun 24, 2005 7:22 am Post subject: Constructors that vary behavior according to constness |
|
|
This seems to be a part missing from C++, perhaps intentionally, or
perhaps I'm missing an obvious work-around.
Can a constructor detect whether it is constructing a const object? For
example, if a class wraps a external object such as a file, the file can
be opened in read-only mode if the constructor is constructing a const
object, e.g.:
const File file("afilename"); // can open the file in read-only mode
File anotherfile("anotherfilename"); // opens the file read-write
Glen Dayton
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
msalters Guest
|
Posted: Fri Jun 24, 2005 11:23 am Post subject: Re: Constructors that vary behavior according to constness |
|
|
dayton schreef:
| Quote: | This seems to be a part missing from C++, perhaps intentionally, or
perhaps I'm missing an obvious work-around.
Can a constructor detect whether it is constructing a const object? For
example, if a class wraps a external object such as a file, the file can
be opened in read-only mode if the constructor is constructing a const
object, e.g.:
const File file("afilename"); // can open the file in read-only mode
File anotherfile("anotherfilename"); // opens the file read-write
|
Would be tricky. Say,
class C { C(int) };
const C c = 1;
Is the constructor creating a const object?
Similar:
void foo( C const& );
foo(1);
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Fri Jun 24, 2005 11:25 am Post subject: Re: Constructors that vary behavior according to constness |
|
|
dayton wrote:
| Quote: | Can a constructor detect whether it is constructing a const object?
|
What is a const object is a matter not only how you create it, but also
how you look at it.
// example 1.
const A *p = new A();
// example 2.
A a;
const A &r = a;
Above, p is a pointer and r is a reference to the "const" object of
class A. Yet the constructor cannot know that this will be intended use
of the object.
| Quote: | For
example, if a class wraps a external object such as a file, the file can
be opened in read-only mode if the constructor is constructing a const
object
|
The fact that the file is constructed in the read-only mode does not
mean that the object representing the file in your program should be
const. There are many different things that can and should change in
this object while you operate on the file, for example read position,
end-of-file flag, format manipulators, conversion policies, state of
multibyte reading state-machine, etc.
In other words - the local representation of some external resource is
not necessarily const even when the resource itself is to be treated as
non-mutable.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gleb Alexeev Guest
|
Posted: Fri Jun 24, 2005 11:25 am Post subject: Re: Constructors that vary behavior according to constness |
|
|
| Quote: | Can a constructor detect whether it is constructing a const object? For
example, if a class wraps a external object such as a file, the file can
be opened in read-only mode if the constructor is constructing a const
object, e.g.:
|
AFAIK, it can't. But I think there's a big difference between read-only
file and const File object -- constness means you can't call functions
modifying File's state, and reading obviously does modify it's state,
changing current read position, probably turning eof flag on, etc.
I thin when you nee such sort of difference, there's not enough to use
const specifier. For instance, all standard containers have 2 distinct
types of iterators, because const vector<>::iterator <>
vector<>::const_iterator.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Sun Jun 26, 2005 2:08 pm Post subject: Re: Constructors that vary behavior according to constness |
|
|
On 24 Jun 2005 03:22:55 -0400, dayton <mvglen04-cnews (AT) yahoo (DOT) com> wrote:
| Quote: | Can a constructor detect whether it is constructing a const object?
|
Only if you help it.
| Quote: | For
example, if a class wraps a external object such as a file, the file can
be opened in read-only mode if the constructor is constructing a const
object, e.g.:
const File file("afilename"); // can open the file in read-only mode
File anotherfile("anotherfilename"); // opens the file read-write
|
They both use the same constructor and do the same thing. You can
create two constructors that do what you want. This may be what you
want or disgusting hackery. In any case it should be amusing.
#include <iostream>
#include <fstream>
using namespace std;
struct File {
fstream f;
File (char* f, File&) : f(f, ios::in | ios::out) {
cout << "non-constn";
}
File (char* f, File const&) : f(f, ios::in) {
cout << "constn";
}
};
int main () {
File const f1("write.it", f1);
File f2("read.it", f2);
}
Some versions of gcc can't handle it, but it is valid.
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|