Re: Newb Question on Properties of Objects
On Dec 2, 1:46 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 2 Dec, 11:51, databi...@gmail.com wrote:
[...]
int main ()
{
Star all_stars(255); //a list holding 256 star objects
His type Star didn't have a constructor taking an int, and it
only represented a single star. What he needs here is:
std::vector< Star > all_stars( 256 ) ;
(Or don't define the dimensions, and just push_back the Star as
you input them.)
Star current; //a single star object to use in program
consider making current a pointer
Why? For the moment, Star seems to have value semantics, which
means that you probably don't want a pointer to it.
int hour = 18;
for (int i = 0; i<=255;i++)
{
current = all_stars(i);
Display_star(current.bearing(hour), current.ascension(hour)); //
function to render stars
}
return 0;
}
So, my question is, will my main() work? Particularly, is it
valid to assign all_stars(i) to "current"?
yes
No, since the definition of itself wasn't legal. If he uses a
vector, then
current = all_stars[ i ] ;
is certainly legal. But I'd definitly write the loop:
for ( size_t i = 0 ; i < all_stars.size() ; ++ i ) {
// ...
}
(Or use an iterator.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34