"Abhi" <ab*******@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
But what actually I need is that, I would like to write the values to
file only for one of the two objects I have. So I kept the constructor
as shown below.
and only if bool is true,which I initialized for one object, I need to
write the values to the file.. so in the constructor, I wrote
constRad("filename.txt") in a "if" condition such as
class className
{
className(bool) //constructor
public:
...
private:
....
}
[...]
className::className(bool boolvar)
{
if(boolvar == true)
constRad("......txt");
}
You are still using a syntax that looks like a function call, but you can do
it only in the constructor initialization list as in
className::className()
:
constRad("......txt")
{}
The problem with your constructor is that, once you are in the constructor
body, constRad is already default-constructed: it is a proper object that is
not associated with a file yet. You shouldn't use the initialization syntax
in the constructor body anymore.
You can open the file though:
className::className(bool boolvar)
{
if (boolvar) // <-- comparing with 'true' is not needed
{
constRad.open("......txt");
}
}
[...]
As an aside, it is better to introduce an enum to make the decision:
enum FileUseDecision { useFile, dontUseFile };
Once you have that, the code will be more clear:
className::className(FileUseDecision decision)
{
if (decision == useFile)
{
constRad.open("......txt");
}
}
Ali