473,549 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.t xt", 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 1377
"John" <jo*********@ya hoo.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.t xt", 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<myc lass *> container;

//...
p_new_class = new myclass;
container.push_ back(p_new_clas s);
// ...
std::vector<myc lass *>::const_itera tor 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.l earn.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::myclas s(){
ifstream infile( "datafile.t xt", 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.********@com Acast.net> wrote in message news:<BTdQb.108 909$nt4.406718@ attbi_s51>...
"John" <jo*********@ya hoo.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.t xt", 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*********@ya hoo.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::myclas s(){
So, you put the file reading into the constructor...
ifstream infile( "datafile.t xt", 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.********@com Acast.net> wrote in message

news:<BTdQb.108 909$nt4.406718@ attbi_s51>...
"John" <jo*********@ya hoo.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.t xt", 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.********@com Acast.net> wrote in message news:<nflQb.137 092$na.216154@a ttbi_s04>...
"John" <jo*********@ya hoo.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::myclas s(){


So, you put the file reading into the constructor...
ifstream infile( "datafile.t xt", 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.********@com Acast.net> wrote in message

news:<BTdQb.108 909$nt4.406718@ attbi_s51>...
"John" <jo*********@ya hoo.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.t xt", 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*********@ya hoo.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.********@com Acast.net> wrote in message news:<6NCQb.140 281$I06.1291768 @attbi_s01>...
"John" <jo*********@ya hoo.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
2459
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 thread? -- Antoon Pardon
7
2082
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 folders.....Here is the scenario and question:: If my remote webhost at (eg): http://69.8.9.9 ....has a subfolder with with:
8
4022
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 problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my...
3
1446
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 object, you must save the pointer of the object in super_auto_ptr //the class cannot work correctly in multithread
5
1341
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 than "beg" will any CD work to install more features. Or, will the add features look for the original CD only. Office 2000 installed and I would...
7
4797
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 AddDomainUserToLocalGroup(string computerName, string groupName, string domainName, string userName) { Hashtable htRet = new Hashtable();...
0
1554
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
4898
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 David Daniels where he said that probably the VS2003 toolkit will be used for Python 2.5 again. However, even before the release of Python 2.5, I...
1
1993
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 work, right? Regards, Ryan
1
1432
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 ending the in ..exe extension (and I don't think anything else). Is there a portable way to check what Popen will work on without actually execute...
0
7518
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1057
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.