 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
KK Guest
|
Posted: Fri Nov 18, 2005 10:29 am Post subject: How to make this snippet an efficient one? |
|
|
Hi, The goal of this program is to define all the objects required by
the program at one place - i.e., at the time of initialization. But I
soon run into problems when an object's intilization function argument
is dependent on another object's attribute which is yet to be defined.
How can I work around this problem? Thank you.
-KK
#define SIZE 100
Class Name
{
int _pos;
int _val;
void InitObj (int pos) : _pos ( pos ) { _val = 0 ; //temporary _val
initialization } ;
void InitValue (int buffer [ SIZE ] ) { _val = buffer [ pos ] ;
//permanant _val initialization } ;
// define other essential funcs
}
class Country
{
Name obj [ 2 ] ;
obj[0].InitObj ( 1 );
/* obj[1].IntiObj(obj[0]._val); // oops! obj[0]._val is not yet
initialized to non-zero value. Is there any way to get around this
problem? */
}
void Function_That_Works(Country& ex)
{
//define buffer & other required variables here
ex.obj[ 0 ].InitValue( buffer );
ex.obj[ 1 ].InitObj( obj[0]._val ); //tedious intialization of the
obj
ex.obj[ 1 ].InitValue( buffer );
}
void Function_That_DOES_NOT_Work(Country& ex)
{
//define buffer & other required variables here
for( int i = 0 ; i < 2 ; i++ )
ex.obj[ i ].InitValue( buffer );
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|