Re: How to share data?

From: James Rogers (jimmaureenrogers_at_att.net)
Date: 01/25/04


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



Relevant Pages

  • Re: How to share data?
    ... Your constructor copies the values ... but you do not know if readdata() has ever been ... This is not a good class interface design. ... > int getdata(); ...
    (comp.programming)
  • Trouble with non-default constructors
    ... I have a class that needs a non-default constructor, ... class MyClass ... int Value; ... But if I try to compile my compilers complains there's no default ...
    (alt.comp.lang.learn.c-cpp)
  • class unable to write the memory
    ... class MyClass{ ... I call the class constructor in the Doc constructor, like I always do, but this time the constructor is unable to modify the memory ...
    (microsoft.public.vc.mfc)
  • Re: C# very optimisation
    ... > You're just not going to see the difference using an int. ... > class MyClass ... > Approx 0.3% faster ... Post-increment loop ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The effect of "^"
    ... public ref class MyClass ... public ValueType SomeNumber; ... public int SomeNumber; ... public IntPtr WindowHandle; ...
    (microsoft.public.dotnet.languages.vc)