Re: How to share data?
From: James Rogers (jimmaureenrogers_at_att.net)
Date: 01/25/04
- Next message: blmblm_at_myrealbox.com: "Re: Response to Karen and to Willem on recursive proofs"
- Previous message: blmblm_at_myrealbox.com: "Re: Flame Bait! Windows vs: The Unices"
- In reply to: John: "Re: How to share data?"
- Next in thread: John: "Re: How to share data?"
- Reply: John: "Re: How to share data?"
- Reply: Programmer Dude: "Re: How to share data?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 24 Jan 2004 23:04:06 GMT
johnw822003@yahoo.com (John) wrote in
news:c30e885a.0401241123.59036779@posting.google.com:
> Thanks.
> Below is my new approach. But it can not pass compile. I do not know
> why.
>
>
> class myclass
> {
> public:
> myclass();
> int getdata (int);
> static void readdata ();
> static int tempdata[10];
>
> protected:
> int data[10];
> };
>
> myclass::myclass ()
> {
> int i = 0;
> for(; i < 10; i++)
> data[i] = tempdata[i];
> }
> void myclass::readdata ()
> {
> int i = 0;
ifstream infile ("data.txt", ios::in);
while (infile && i < 10)
infile >> tempdata[i++];
> }
>
> int myclass::getdata (int i)
> {
> return (data[i]);
> }
>
A few questions:
Why is readdata() public? It should only be called once, the first time the
constructor is called.
You have a timing issue in your design. Your constructor copies the values
from tempdata to data, but you do not know if readdata() has ever been
called. It will not have been called the first time around unless you
expect the user of this class to call readdata() before creating the first
instance of the class. This is not a good class interface design.
class myclass
{
public:
myclass();
int getdata();
private:
static int tempdata[10];
static boolean initialized = false;
protected:
int data[10];
}
myclass:myclass()
{
int i;
if (!initialized)
{
initialized = true;
i = 0;
ifstream infile ("data.txt", ios::in);
while (infile && i < 10)
infile >> tempdata[i++];
close(infile);
}
for(i = 0; i < 10; i++)
data[i] = tempdata[i];
}
int myclass:getdata(int i)
{
return data(i);
}
I apologize for any syntax errors. I am more experienced in
Java and Ada than in C++.
Note that the constructor handles all static data initialization
issues. Furthermore, it only reads file once.
I have made tempdata private because it should only be accessed
by the class constructor and member functions.
This design eliminates api timing issues. It also eliminates
the readdata() function.
Jim Rogers
- Next message: blmblm_at_myrealbox.com: "Re: Response to Karen and to Willem on recursive proofs"
- Previous message: blmblm_at_myrealbox.com: "Re: Flame Bait! Windows vs: The Unices"
- In reply to: John: "Re: How to share data?"
- Next in thread: John: "Re: How to share data?"
- Reply: John: "Re: How to share data?"
- Reply: Programmer Dude: "Re: How to share data?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|