-
December 10th, 2008, 03:22 PM
#1
Returning contents of int array from function
Grr, cant seem to get this to work..
I have a class that has
The header has
int* puzzle;
The constructor has
puzzle = new int[81];
I want to have a member that returns the contents of the 81 element array and not a pointer to it.. Basically after the class is created and some other stuff happends, the class is deleted.. Before the class is deleted I need to get the contents of the array out of the class and "copy" it to a local variable so I will have it after the class is deleted..
How can I do this?
-
December 10th, 2008, 03:29 PM
#2
Re: Returning contents of int array from function
Make your life ALOT easier...STOP using naked pointers and "raw" arrays. In this case std::vector<int> is your friend.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 10th, 2008, 03:48 PM
#3
Re: Returning contents of int array from function
vector would make your life easier. The alternative would be to write a function that copies the value from one array into another.
-
December 10th, 2008, 05:03 PM
#4
Re: Returning contents of int array from function
Note that arrays are not copyable, so it's impossible to move them around in any way except (a) passing a pointer or (b) copying one array into another.
std::vector wraps up these operations in a way that makes it *look* to the user like a copyable array, which is one of many reasons why it makes things easier.
-
December 11th, 2008, 02:20 AM
#5
Re: Returning contents of int array from function
-
December 11th, 2008, 04:59 AM
#6
Re: Returning contents of int array from function
Originally Posted by TheCPUWizard
Make your life ALOT easier...STOP using naked pointers and "raw" arrays. In this case std::vector<int> is your friend.
I'm waiting for the ubiquitous "my teacher won't allow me to use vector" comment any moment now...
Regards.
Paul McKenzie
-
December 11th, 2008, 02:10 PM
#7
Re: Returning contents of int array from function
Originally Posted by Paul McKenzie
I'm waiting for the ubiquitous "my teacher won't allow me to use vector" comment any moment now...
A completely off topic question...
Do you guys [Paul, CPUWiz, etc] think that things like naked pointers and arrays shouldn't be taught, or something? I fully agree that vector and most other things STL make using C++ much easier, lead to less bugs, easier code maintenance and so on, but it still seems to me that one must know these things to be considered a proficient C++ programmer. Even if you never plan to write a single line of your own code using them, you have to know them because you simply can't escape them. Whether it's maintaining legacy code, using 3rd party libraries, or whatever...
Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful. Because, I mean, if the guy is in a class and he is supposed to be learning about pointers, your comment just seems just slightly rediculous and/or arrogant.
-
December 11th, 2008, 02:31 PM
#8
Re: Returning contents of int array from function
Speeking just for myself, I agree that a person must know and understand all of the "C"/"C-Style", but my experience as a trainer is that teaching a person how to USE collections, string etc (ie STL) and THEN teaching them how to implement the constructs is MUCH more effective.
Which did you learn first? How ride a bicycle or how to rebuild a 10 speed shifter?
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 11th, 2008, 02:35 PM
#9
Re: Returning contents of int array from function
Seems to me a lot of the instructors are basically teaching C using a C++ compiler. You can't use C++ without understanding pointers and C style strings, but the language has evolved over the last 20 years. From what we see here, I'd conclude that a lot of instructors are stuck in the past.
It would make more sense to teach the build in list, vector, set, string, etc classes than to teach how to do it in C at least for beginners. Sure, it's helpful to know how a list or set is implemented, but not right of the bat.
If posts seem arrogant, it's not directed at the poster. It's directed at the instructors who are still teaching the language as it existed in two decades ago.
-
December 11th, 2008, 11:11 PM
#10
Re: Returning contents of int array from function
Originally Posted by Speedo
Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful.
If I were to consider to intern/novice applicants that were identical in all resepects except #1 knew how to use STL effectively, but was very very poor on pointers and dynnamic memory management, while #2 knew pointers, and robust memory management very well, but was not able to use STL effectively....
I would hire the first one over the second.
The reason is simple. I can have a slightly more senior person handle the low level code for a project, but nearly EVERY method in our systems includs STL constructs and concepts.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 12th, 2008, 01:58 AM
#11
Re: Returning contents of int array from function
Originally Posted by Speedo
A completely off topic question...
Do you guys [Paul, CPUWiz, etc] think that things like naked pointers and arrays shouldn't be taught, or something? I fully agree that vector and most other things STL make using C++ much easier, lead to less bugs, easier code maintenance and so on, but it still seems to me that one must know these things to be considered a proficient C++ programmer. Even if you never plan to write a single line of your own code using them, you have to know them because you simply can't escape them. Whether it's maintaining legacy code, using 3rd party libraries, or whatever...
Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful. Because, I mean, if the guy is in a class and he is supposed to be learning about pointers, your comment just seems just slightly rediculous and/or arrogant.
It's one thing to learn and know how to use pointers, and a completely different thing to overuse them, even when a better approach, like using an STL container, is available and desirable.
-
December 12th, 2008, 08:44 AM
#12
Re: Returning contents of int array from function
Originally Posted by Speedo
Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers? Pardon me if I'm doubtful. Because, I mean, if the guy is in a class and he is supposed to be learning about pointers, your comment just seems just slightly rediculous and/or arrogant.
The author of the C++ language, Bjarne Stroustrup, must also be ridiculous and arrogant.
http://www.research.att.com/~bs/new_learning.pdf
Regards,
Paul McKenzie
-
December 12th, 2008, 08:52 AM
#13
Re: Returning contents of int array from function
Originally Posted by Speedo
Would you guys -any of you- seriously hire a C++ programmer who was a master of std::vector but knew nothing at all about raw pointers?
This is a very rare individual. I don't even know if such a person exists. First off, they would have learned the iterator concept, which means they know what "dereferencing" means, which leads them into knowing what pointers are and what they do. In addition, pointers are used for polymorphism (i.e. virtual functions). There is no such thing as virtual functions in C, so the likelihood that an ace C++ programmer who knows STL and at the same time doesn't know pointers is so remote, it shouldn't even be discussed.
The other way around, where someone knows all the low-level details of pointers and know nothing about STL. Forget it -- he isn't hired by me. I know a lot of 'C' programmers who try to learn STL and can never get it (iterators, containers, algorithms), and all of these are fundamental in being a C++ programmer. This happens far more often than the first scenario you discussed about C++ programmers who know STL and do not know pointers.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 12th, 2008 at 09:23 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|