Advertisement

How would i pass an array of a class to a function?

Started by March 07, 2003 08:08 AM
35 comments, last by StormUK 21 years, 8 months ago
THanks..i think im getting my head round it now =)
im getting a few errors, but surely its my coding =)
quote: Original post by StormUK
Title says it all - here is what i have tried so far

Unfortunately, the responses you''ve received are below average even for the low standards of the Gamedev For Beginner''s forum! Here''s some comments that you didn''t ask for...


  • Prefer the header "iostream" over "iostream.h"

  • Don''t use character arrays for storing strings. Use std::string instead. If your learning text doesn''t mention std::string, then throw it away and get a decent one.

  • Look up "initialiser lists"



To answer your immediate question, don''t use arrays at all. Arrays are a really dumb idea that''s been retained in C++ via C since the early seventies. A much better modern version of arrays is std::vector. You can read about std::vector here. Using vectors will make it very easy for you to pass arrays of data around.
Advertisement
I''ll have a look.

Thanks =)
Chris
But then again, learning the language before starting
to learn the assorted intricacies of STL is strongly
recommended...

--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
quote: Original post by tok_junior
But then again, learning the language before starting
to learn the assorted intricacies of STL is strongly
recommended...

By who?
By anyone who agrees that you should learn to crawl before you try to run.

--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
Advertisement
quote: Original post by tok_junior
By anyone who agrees that you should learn to crawl before you try to run.

Really? Well, I agree with that, but I don''t agree that you shouldn''t learn appropriate Standard Library features early on. Your appeal to popularity seems to have broken.
STL are templates, which aren''t exactly considered c++ basics. Plus, they''re not applicable in all situations.
He''d be better off learning the language, and then using the bonuses.

--
MFC is sorta like the swedish police... It''''s full of crap, and nothing can communicate with anything else.
I''m with tok_junior on this one. I''ve been programming c++ for over 2 years. Only within the last couple months have I done any STL other than strings, which I started using about a year ago. Part of the reason is probably because the book I used didn''t have anything about STL. Nonetheless, I know that I would not have been able to use STL properly when I was still learning.
quote: Original post by tok_junior
STL are templates, which aren't exactly considered c++ basics.

You don't have to know how to develop templates to be a consumer of templated code. For example, to use a vector of ints, you write:
std::vector<int> v;  

After which, you do not need to know anything about templates. This is hardly any more difficult to learn than:
int ar[MAGIC_CONSTANT];  

And a vector has tangible benefits over an array, particularly for newbies. For example, you can pass vectors around in a non-braindamaged manner.
quote:
Plus, they're not applicable in all situations.

Who said they were? No one feature is applicable in all situations, so your comment amounts to nothing.
quote:
He'd be better off learning the language, and then using the bonuses.

Who says the C++ Standard Library is some sort of "bonus"? What nonsense is that? Your claim is along the lines of saying a beginner has to learn to write programs without being allowed to #include any standard headers, before they are then allowed to go on and use the standard library as it was intended. How do you support such a nonsensical position?

quote: Original post by AikonIV
I'm with tok_junior on this one.

You and tok_junior have your work cut out.
quote:
I've been programming c++ for over 2 years. Only within the last couple months have I done any STL other than strings, which I started using about a year ago. Part of the reason is probably because the book I used didn't have anything about STL.
I'm not talking specifically about the STL, I'm talking about the C++ Standard Library of which std::vector and std::string are a part. Having a deficient book is a reason to throw away the book, not to persevere with bad programming practices.

Nonetheless, I know that I would not have been able to use STL properly when I was still learning.

I doubt that you had the ability to use *any* feature properly when you were first learning. That's no argument. The point of learning is that you find out how to use things correctly. Learning to use std::vector is no more difficult than learning to use arrays. In fact, I'm prepared to argue std::vector is easier to use, since it behaves how arrays *should* behave, and how newbies assume they behave, whereas arrays are based on 30+ year old idiocy based on vague efficiency concerns. There's utterly no reason to burden newcomers with troublesome, painstaking learning processes just because it's how you did it. There's no arcane rite of passage to learning C++, where you must take a long trek through all the idiotic features before arriving at the sensible stuff.

[edited by - SabreMan on March 8, 2003 11:43:30 AM]

This topic is closed to new replies.

Advertisement