473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Will it work?

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] ) ++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
Jul 22 '05 #1
7 1370
"John" <jo*********@yahoo.com> wrote...
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] ) ++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
Jul 22 '05 #2
John wrote:
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

Jul 22 '05 #3
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" <v.********@comAcast.net> wrote in message news:<BTdQb.108909$nt4.406718@attbi_s51>...
"John" <jo*********@yahoo.com> wrote...
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] ) ++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

Jul 22 '05 #4
"John" <jo*********@yahoo.com> wrote...
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" <v.********@comAcast.net> wrote in message

news:<BTdQb.108909$nt4.406718@attbi_s51>...
"John" <jo*********@yahoo.com> wrote...
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] ) ++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

Jul 22 '05 #5
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<nflQb.137092$na.216154@attbi_s04>...
"John" <jo*********@yahoo.com> wrote...
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" <v.********@comAcast.net> wrote in message

news:<BTdQb.108909$nt4.406718@attbi_s51>...
"John" <jo*********@yahoo.com> wrote...
> 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] ) ++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

Jul 22 '05 #6
"John" <jo*********@yahoo.com> wrote...
[...]
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
Jul 22 '05 #7
Hi Victor:

Thanks a lot. I got the trick.

John

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<6NCQb.140281$I06.1291768@attbi_s01>...
"John" <jo*********@yahoo.com> wrote...
[...]
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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
7
by: jason | last post by:
I am getting twisted by the possibility that my virtual includes which currently work great on non-domain remote IP will crash if I purchase a domain and point it to one of my designated...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
3
by: Zheng Da | last post by:
Will the following class work well? If it can not work correctly in some situation, could you help me to fix it? Thank you. //the class will work like the reference in java //when you create a...
5
by: Ralph2 | last post by:
Hello The person responsible for installing software at work believes in installing the bare minimum., which does not include the option to split a data base into a front and back end. So, rather...
7
by: Peter Steele | last post by:
I have code to add a domain user to a local group but I'm not sure if it will work with NT domains or whether it will only work with Active Directory based systems. Here's the code: public void...
0
by: palomine1234 | last post by:
PAYPAL MAGIC!!! TURN $5 INTO $15,000 IN ONLY 30 DAYS...HERES HOW! This is a Money Scheme and Not, I repeat... This is Not a Scam!!!
48
by: meyer | last post by:
Hi everyone, which compiler will Python 2.5 on Windows (Intel) be built with? I notice that Python 2.4 apparently has been built with the VS2003 toolkit compiler, and I read a post from Scott...
1
by: Ryan Liu | last post by:
So for a UtpClient to JoinMulticastGroup, there must be a router in the network and with multicast enabled? That is, for a small LAN with a few computers connected by a hub or siwtch, it won't...
1
by: Brendan Miller | last post by:
I need a portable way to tell what subprocess.Popen will call. For instance on unix systems, Popen will work for files flagged with the executable bit, whereas on windows Popen will work on files...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.