 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mirek Fidler Guest
|
Posted: Fri Jun 27, 2003 4:50 pm Post subject: Re: Destructors with arguments revisited |
|
|
| Quote: | I understand why destructors with arguments are difficult but I'd
really like one for the following situation:
class data { int x; };
class dataStreamer {
~dataStreamer( data d ) { // save data, then destruct }
};
The idea is that dataStreamer cannot be destroyed unless it's given
data to save away. Any circumstance that that would lead to a call to
dataStreamer::~dataStreamer without arguments should lead to a compile
or link error.
Are there better ways to achieve the same result?
|
What about
class dataStream {
dataStream(data d);
};
?
Certainly, I would need to know more about your specific situation,
but if passing single "data" is all you require, it is no difference
between passing it in constructor or destructor.
Mirek
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Fri Jun 27, 2003 6:12 pm Post subject: Re: Destructors with arguments revisited |
|
|
[email]andrew_queisser (AT) hp (DOT) com[/email] (andrew queisser) wrote in message news:<ad2f1094.0306260955.2c7a5120 (AT) posting (DOT) google.com>...
| Quote: | The idea is that dataStreamer cannot be destroyed unless it's given
data to save away. Any circumstance that that would lead to a call to
dataStreamer::~dataStreamer without arguments should lead to a compile
or link error.
Are there better ways to achieve the same result?
|
The problem is that there are too many ways to destruct an object. If
it is on the heap, you call delete. If it is a member of a class, it
is destructed when the class is destroyed. If it is on the stack, it
is destructed at the corresponding close brace, or when an exception
is thrown.
Consider this code:
{
dataStream object;
//more code
}
if you want to pass "data" to the destructor, where do you do it? If
you do it at the close brace, then what happens if "//more code"
throws an exception? Then how do you specify data?
Finally, destructors are for cleaning things up. You shouldn't be
doing anything complex which might possibly fail.
The only way to do what you want is to pass data in to the
constructor. Make it a const data member, thus requiring all
constructors to initialize the value. Thus, you are guaranteed that
"data" is valid when you reach the destructor, no matter how you get
there.
joshua lehrer
factset research systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jeff Greif Guest
|
Posted: Sat Jun 28, 2003 12:01 am Post subject: Re: Destructors with arguments revisited |
|
|
"andrew queisser" <andrew_queisser (AT) hp (DOT) com> wrote
| Quote: | I understand why destructors with arguments are difficult but I'd
really like one for the following situation:
class data { int x; };
class dataStreamer {
~dataStreamer( data d ) { // save data, then destruct }
};
The idea is that dataStreamer cannot be destroyed unless it's given
data to save away. Any circumstance that that would lead to a call to
dataStreamer::~dataStreamer without arguments should lead to a compile
or link error.
Are there better ways to achieve the same result?
|
Sure. The standard pattern is to give it the data in the constructor.
If
the dataStreamer is an automatic object, it will be destroyed at the end
of
the block in which it is created. If you wish to put the saving of the
data
in the destructor, you may. The implicit semantics of your dataStreamer
object is that exists for the purpose of saving one data object, so you
might as well create it specifically to do so and pass the data in the
constructor arguments.
Jeff
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sat Jun 28, 2003 11:12 pm Post subject: Re: Destructors with arguments revisited |
|
|
"andrew queisser" <andrew_queisser (AT) hp (DOT) com> wrote in message
| Quote: | class data { int x; };
class dataStreamer {
~dataStreamer( data d ) { // save data, then destruct }
};
The idea is that dataStreamer cannot be destroyed unless it's given
data to save away. Any circumstance that that would lead to a call to
dataStreamer::~dataStreamer without arguments should lead to a compile
or link error.
|
Everyone else so far pointed out to do it in the constructor. You can also
do it in a member function. For example consider std::ifstream.
void f() {
std::ifstream f("hello.txt");
}
The constructor opens a file with name "hello.txt". The destructor, called
implicitly at the closing brace }, flushes (transfers information written to
memory to the actual file) and closes the file.
You can also do
void f() {
std::ifstream f;
if (condition()) {
f.open("hello.txt");
}
}
In this case, if condition() returns false then the file is never opened and
the destructor, still implcitly called at the end, does nothing.
--
+++++++++++
Siemel Naran
[ 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
|
|