Will it work?

J

John

Hi:

I want all objects of a class share the same set of data, which will
be input from a file when the code start to run. In my design, I use
static data member.
The definition of the class is as below:

class myclass{
public:
static int data[100]; //The shared data.
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while ( infile >> data ) ++i; //Input data from the file.

//Definition of other data member and member functions.
void funct1();
void funct2();
char name[10];

protected:
.....
}

When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 share the same data.

Will my design work? If possible, please give me your suggestion.
By the way, this is not a homework question. :) I am working on a
project and considering how to share data.

Thanks a lot.

John
 
V

Victor Bazarov

John said:
I want all objects of a class share the same set of data, which will
be input from a file when the code start to run. In my design, I use
static data member.
The definition of the class is as below:

class myclass{
public:
static int data[100]; //The shared data.
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while ( infile >> data ) ++i; //Input data from the file.



This is not C++. You cannot have executable code in the class
definition unless it's in a function definition. Besides, you
might want to look into

while (infile && (i < 100))
infile >> data[i++];

But, again, not directly in the class definition.
//Definition of other data member and member functions.
void funct1();
void funct2();
char name[10];

protected:
.....
}

When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 share the same data.

Will my design work?

Not likely. You should look into a static data member which,
when initialised, will fill in the 'data' array.
If possible, please give me your suggestion.
By the way, this is not a homework question. :) I am working on a
project and considering how to share data.

OK

Victor
 
T

Thomas Matthews

John said:
Hi:
[snip]

When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

I don't understand why you are using dynamic allocation.
Is this a leftover from Java?

If you have a lot of instances, you should use a container class
such as vector, list, set, map, etc.

With a vector:
myclass * p_new_class;
std::vector<myclass *> container;

//...
p_new_class = new myclass;
container.push_back(p_new_class);
// ...
std::vector<myclass *>::const_iterator c_iter;
for (c_iter = container.begin();
c_iter != container.end();
++c_iter)
{
// process an element
cout << (*iter) << "\n";
}

c_1, c_2,..., c_100 share the same data.


Will my design work? If possible, please give me your suggestion.
By the way, this is not a homework question. :) I am working on a
project and considering how to share data.

Thanks a lot.

John


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

John

I got a new design. Will it work?

class myclass{
public:
void myclass();
int data[100]; //The shared data.
void funct1();
void funct2();
char name[10];

protected:
.....
}

void myclass::myclass(){
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while (infile && (i < 100))
infile >> data[i++]; //Input data from the file.
}


When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 read from the same file and share the same data.



Victor Bazarov said:
John said:
I want all objects of a class share the same set of data, which will
be input from a file when the code start to run. In my design, I use
static data member.
The definition of the class is as below:

class myclass{
public:
static int data[100]; //The shared data.
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while ( infile >> data ) ++i; //Input data from the file.



This is not C++. You cannot have executable code in the class
definition unless it's in a function definition. Besides, you
might want to look into

while (infile && (i < 100))
infile >> data[i++];

But, again, not directly in the class definition.
//Definition of other data member and member functions.
void funct1();
void funct2();
char name[10];

protected:
.....
}

When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 share the same data.

Will my design work?

Not likely. You should look into a static data member which,
when initialised, will fill in the 'data' array.
If possible, please give me your suggestion.
By the way, this is not a homework question. :) I am working on a
project and considering how to share data.

OK

Victor
 
V

Victor Bazarov

John said:
I got a new design. Will it work?

Yes, but at what price!
class myclass{
public:
void myclass();
int data[100]; //The shared data.
void funct1();
void funct2();
char name[10];

protected:
.....
}

void myclass::myclass(){

So, you put the file reading into the constructor...
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while (infile && (i < 100))
infile >> data[i++]; //Input data from the file.
}


When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 read from the same file and share the same data.

And since they are constructed 100 times, the file will be opened
and read 100 times. WHY? WHAT FOR?

Create a static function "initData" and a static int (just for the heck
of it). Then when initialising the static int, call the 'initData'
function. This way you will only initialise the array ONCE, when the
static int is initialised.

Do I need spell it out in C++ or can you handle it yourself?
"Victor Bazarov" <[email protected]> wrote in message
John said:
I want all objects of a class share the same set of data, which will
be input from a file when the code start to run. In my design, I use
static data member.
The definition of the class is as below:

class myclass{
public:
static int data[100]; //The shared data.
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while ( infile >> data ) ++i; //Input data from the file.



This is not C++. You cannot have executable code in the class
definition unless it's in a function definition. Besides, you
might want to look into

while (infile && (i < 100))
infile >> data[i++];

But, again, not directly in the class definition.
//Definition of other data member and member functions.
void funct1();
void funct2();
char name[10];

protected:
.....
}

When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 share the same data.

Will my design work?

Not likely. You should look into a static data member which,
when initialised, will fill in the 'data' array.
If possible, please give me your suggestion.
By the way, this is not a homework question. :) I am working on a
project and considering how to share data.

OK

Victor
 
J

John

Victor Bazarov said:
John said:
I got a new design. Will it work?

Yes, but at what price!
class myclass{
public:
void myclass();
int data[100]; //The shared data.
void funct1();
void funct2();
char name[10];

protected:
.....
}

void myclass::myclass(){

So, you put the file reading into the constructor...
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while (infile && (i < 100))
infile >> data[i++]; //Input data from the file.
}


When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 read from the same file and share the same data.

And since they are constructed 100 times, the file will be opened
and read 100 times. WHY? WHAT FOR?

Create a static function "initData" and a static int (just for the heck
of it). Then when initialising the static int, call the 'initData'
function. This way you will only initialise the array ONCE, when the
static int is initialised.

Do I need spell it out in C++ or can you handle it yourself?

Sorry, I can not figure it out. I just start to learn C++.
When to access the file and read data? Could you please spell it out? :)

John
"Victor Bazarov" <[email protected]> wrote in message
I want all objects of a class share the same set of data, which will
be input from a file when the code start to run. In my design, I use
static data member.
The definition of the class is as below:

class myclass{
public:
static int data[100]; //The shared data.
ifstream infile( "datafile.txt", ios::in );
int i = 0;
while ( infile >> data ) ++i; //Input data from the file.


This is not C++. You cannot have executable code in the class
definition unless it's in a function definition. Besides, you
might want to look into

while (infile && (i < 100))
infile >> data[i++];

But, again, not directly in the class definition.


//Definition of other data member and member functions.
void funct1();
void funct2();
char name[10];

protected:
.....
}

When the code runs, the data will be read in from the file. The
following objects:

myclass *c_1 = new myclass;
myclass *c_2 = new myclass;
.....
myclass *c_100 = new myclass;

c_1, c_2,..., c_100 share the same data.

Will my design work?

Not likely. You should look into a static data member which,
when initialised, will fill in the 'data' array.

If possible, please give me your suggestion.
By the way, this is not a homework question. :) I am working on a
project and considering how to share data.

OK

Victor
 
V

Victor Bazarov

John said:
[...]
Sorry, I can not figure it out. I just start to learn C++.
When to access the file and read data? Could you please spell it out? :)

class A {
static int data[10000000];
static int dummy;
static int init_data() {
// open the file
// read the contents into 'data' array
return 42;
}
public:
A() { // normal constructor, no 'data' manipulation
// blah blah
}
};

int A::dummy = A::init_data(); // 'dummy' initialised only once,
// so, file is only read once
// and millions of 'data' elements
// are only assigned once
// not every time an A is created

Victor
 
J

John

Hi Victor:

Thanks a lot. I got the trick.

John

Victor Bazarov said:
John said:
[...]
Sorry, I can not figure it out. I just start to learn C++.
When to access the file and read data? Could you please spell it out? :)

class A {
static int data[10000000];
static int dummy;
static int init_data() {
// open the file
// read the contents into 'data' array
return 42;
}
public:
A() { // normal constructor, no 'data' manipulation
// blah blah
}
};

int A::dummy = A::init_data(); // 'dummy' initialised only once,
// so, file is only read once
// and millions of 'data' elements
// are only assigned once
// not every time an A is created

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top