|
I have a class with an array as a private member. What's the proper way to write a getter for that member? If I just return the private member array, am I not then returning a pointer to the private member itself, allowing it to be modified by whatever called the getter?
|
# ¿ Jan 31, 2014 04:54 |
|
|
# ¿ Apr 29, 2024 12:38 |
|
Dren posted:Are you in c or c++ and would you be ok with returing a copy to the user? A copy would be my best bet, I think.
|
# ¿ Jan 31, 2014 05:03 |
|
WaterIsPoison posted:What's the point of learning/using C++ if you can't use any of it's features/best practices? Seriously, if your instructor wants you to learn C, they should just teach you C.
|
# ¿ Feb 10, 2014 07:57 |
|
It looks like you need to dynamically allocate an array since the length isn't a compile-time constant. You need to do a heap allocation if the size is to be determined at run-time.
|
# ¿ Mar 29, 2014 00:48 |
|
Xerophyte posted:In C++03:
|
# ¿ Mar 29, 2014 01:02 |
|
He should be using initialization lists instead of assignment inside of the constructors anyway, but then there are a ton of "best practices" issues with that code so I probably shouldn't open a can of worms of nitpicking.
|
# ¿ Mar 31, 2014 21:35 |
|
Tusen Takk posted:do you mean mfgName = mfg and dealerCost = cost? edit: Also I can tell you right now that this instructor sucks. It looks like he's teaching you bad C++98 with no regard for more recent standards or best practices. Star War Sex Parrot fucked around with this message at 21:53 on Mar 31, 2014 |
# ¿ Mar 31, 2014 21:51 |
|
Tusen Takk posted:Also lol at my professor for using unsigned integers? I was under the impression that unsigned ints are hilariously bad practice. Star War Sex Parrot fucked around with this message at 02:22 on Apr 8, 2014 |
# ¿ Apr 8, 2014 02:18 |
|
Tusen Takk posted:In EvE Online the turbonerds are always "lol unsigned ints" when you have a stack of object x greater than whatever the largest number an unsigned int can accept or something like that.
|
# ¿ Apr 8, 2014 02:24 |
|
Every for loop that I write uses an unsigned short int.
|
# ¿ Apr 8, 2014 04:53 |
|
I love C++.
|
# ¿ Apr 15, 2014 01:30 |
|
rjmccall posted:Also I have no idea what language that thing you're doing with -> is even supposed to be from, but it ain't C++. Subjunctive posted:Are you sure that -> thing isn't just pseudocode? C++ code:
Star War Sex Parrot posted:I love C++. Star War Sex Parrot fucked around with this message at 02:51 on Apr 15, 2014 |
# ¿ Apr 15, 2014 02:23 |
|
Tusen Takk posted:Basically this. Coming from Java, it was an especially curvy curveball thrown at me and I'm honestly glad that I'm done with it.
|
# ¿ Apr 15, 2014 07:16 |
|
Tusen Takk posted:I've heard it's basically the best parts of Java and C++, c/d? Either way, yes C# is related to both Java and C++ in some ways and I find it to be a very nice language and the .NET Library to be well put together and all have their places in the world. We now return you to C++ chat.
|
# ¿ Apr 15, 2014 07:46 |
|
Now I'm just left wondering who owns colour and is eventually going to clean it up. Or worse: if it might get deleted before Ball is done with it. edit: I guess that's what kicked off this whole thing and I should do more than just skim the code in the thread. qntm posted:As soon as execution returns from getOneMessage(), colour is taken off the stack, but the returned Ball still retains a pointer to it. Later, when the Ball tries to use the pointer, everything explodes. Obviously, my mistake was to assume that the constructor would take a copy of colour for its own use. Instead I need to make sure that colour remains on the stack (or heap) for at least as long as the returned Ball does.
|
# ¿ May 7, 2014 20:18 |
|
Deus Rex posted:If you have a Mac Dash gives you handy offline access to cppreference among like a hundred other documentation sets. Thanks.
|
# ¿ Jun 5, 2014 23:41 |
|
Vanadium posted:I'd expect to see exceptions used in situation where the code that notices the error doesn't know how to handle it, but some code way up in the callstack does, and exceptions are the best way to communicate the error upwards because throw immediately starts unwinding and jumps up the stack to the handler. In your example, you already know what you're gonna do about the error, so you might as well do it right away, and your throw surprisingly doesn't unwind anything or jump anywhere since the handler is basically in the next line anyway. Hammerite, what you did could easily just be handled without throwing an exception in the first place and instead just using normal control statements.
|
# ¿ Jun 6, 2014 20:14 |
|
You also have to be careful with exceptions in C++ because if you're not adhering to RAII and throw an exception and don't remember to clean stuff up with the handler, you will leak memory.
|
# ¿ Jun 6, 2014 21:03 |
|
I'm fairly certain that if whatever is in the try block generates an exception, execution will enter the catch, do some stuff, and then pass the same exception. In theory I think it's a way to do something when an exception is thrown while leaving the actual handling to another point in the program.
|
# ¿ Jun 10, 2014 17:47 |
|
Destructors for base classes should generally be made virtual so as to play nice with polymorphism. I assume that's what it's yelling at you about. Also getters should probably be const functions so the compiler yells at you if you do something dumb like try to modify ivars in a getter. edit: for the virtual destructor, if you don't actually want to write the implementation, just use "= default"; C++ code:
|
# ¿ Jul 7, 2014 23:57 |
|
Xeom posted:At first it seemed like a way to have functions generate arrays, but everything inside the function goes away once its done being called. I am having a hard time thinking of how to use this guy. edit: let us know specifically what parts are confusing to you and we'll whip up some code samples. Also I hesitate to use auto when first learning C++ as you really need to be aware of the types of your variables. Here's a good code sample that demonstrates dynamic allocation of arrays. Star War Sex Parrot fucked around with this message at 23:39 on Jul 8, 2014 |
# ¿ Jul 8, 2014 23:34 |
|
Subjunctive posted:The link in the OP 404s, and I'm in the market for a book to help me absorb C++11/14 improvements and changes in preferred style (like, no new use?!). Kindle strongly preferred, I'm not likely to lug around a paper copy. Star War Sex Parrot fucked around with this message at 21:53 on Jul 14, 2014 |
# ¿ Jul 14, 2014 21:50 |
|
Edit: never mind that's an initializer list you're using, so my comment is incorrect.
Star War Sex Parrot fucked around with this message at 01:07 on Jul 23, 2014 |
# ¿ Jul 23, 2014 00:57 |
|
Standard practice these days seems to be to just use auto when dealing with a type for iterators.
|
# ¿ Jul 25, 2014 07:00 |
|
ExcessBLarg! posted:That's weird, C isn't really the right language to teach to non-majors. There was a brief period where Pascal fell out of favor in education and folks were still suspicious of Java to teach the introductory level, but there's so many more legitimately-useful options if you're going to teach someone one language that they may actually use later. Star War Sex Parrot fucked around with this message at 23:46 on Aug 19, 2014 |
# ¿ Aug 19, 2014 23:08 |
|
Boz0r posted:I'm generating random input for a convex hull algorithm in 3D, and when the input list gets large enough, I get a bad_alloc error when n > n^24 even though I haven't run out of RAM or anything. C++ code:
matt@sandbox : ~/projects/random $ ./run -0.829935 0.783223 -0.62062 -0.203983 0.487025 0.12078 0.619133 0.0234251 0.990169 0.933223 -0.147898 0.305997 matt@sandbox : ~/projects/random $ ./run -0.829935 0.783223 -0.62062 -0.203983 0.487025 0.12078 0.619133 0.0234251 0.990169 0.933223 -0.147898 0.305997 matt@sandbox : ~/projects/random $ ./run -0.829935 0.783223 -0.62062 -0.203983 0.487025 0.12078 0.619133 0.0234251 0.990169 0.933223 -0.147898 0.305997 See the problem? Now, change your random engine instantiation to: C++ code:
matt@sandbox : ~/projects/random $ ./run -0.742602 0.557945 0.370006 0.509726 -0.412112 -0.175772 0.366924 0.630628 0.172798 0.393836 0.653487 -0.638336 matt@sandbox : ~/projects/random $ ./run 0.0349932 0.553798 0.1745 -0.650975 -0.0515622 0.343564 0.65945 -0.0723755 0.398404 0.550028 -0.295123 0.385876 matt@sandbox : ~/projects/random $ ./run 0.520096 -0.423067 0.853396 0.282208 0.62926 0.144895 0.416956 -0.827151 0.300816 0.0987096 0.373157 0.4119 That's much better. If you want to learn more about C++11's random devices, engines, and distributions, I highly recommend reading this short paper. Star War Sex Parrot fucked around with this message at 05:40 on Oct 1, 2014 |
# ¿ Oct 1, 2014 05:34 |
|
That looks like a circular dependency to me.
|
# ¿ Oct 11, 2014 02:06 |
|
PRADA SLUT posted:if (number < 10) { sysCommand += number; } is the problem.
|
# ¿ Nov 20, 2014 21:01 |
|
Jewel posted:I don't think there's any reason to pass a pointer by reference
|
# ¿ Dec 13, 2014 07:37 |
|
VikingofRock posted:I've run into this before in situations where some member is the result of some expression that depends on another member. Like so: Star War Sex Parrot fucked around with this message at 02:50 on Jan 1, 2015 |
# ¿ Jan 1, 2015 02:48 |
|
jiffypop45 posted:If I'm trying to convert a class function to a global function that has friendship to the class, am I going to be more or less required to pass the object by reference to every single one of those functions? This seems like a rather pedantic exercise for the sake of understanding friendship
|
# ¿ Feb 14, 2015 22:35 |
|
Soylent Pudding posted:I'm familiar with overloading, but my insert function is only supposed to take in a Comparable. The overall problem is that the given input string will sometimes have to be parsed into integer, sometimes to string, and sometimes to any other data type our professor deems appropriate. So what I really need to figure out is how to assign the parsed value into my Comparable var. Perhaps you should ask your instructor/a TA about how to approach this issue for your assignment. edit: also are you saying that input could be "3 4 2 9 38 10 1" which would insert int into your BST, while "Matthew Mark Luke John" which would insert std::string into your BST? Star War Sex Parrot fucked around with this message at 04:29 on Apr 2, 2015 |
# ¿ Apr 2, 2015 04:26 |
|
So the issue at the moment is that when you're tokenizing your input string, you're attempting to make your token (c) represent the type of your template parameter. This seems like the incorrect approach to me because substr is always gonna spit out another string. I'd tokenize to strings first, and then work from there.
|
# ¿ Apr 2, 2015 04:38 |
|
Alternatively, after thinking about it a bit more: rather than tokenizing to std::string using substr(), use a std::istringstream and >> to extract the data in the correct format (either int or std::string). My hunch is that if you >> into a Comparable object then things will work out nicely as long as the template parameter type is int or std::string, but I can't say for certain without mucking with your code a bit. See the example at the bottom of this page for inspiration. Star War Sex Parrot fucked around with this message at 05:23 on Apr 2, 2015 |
# ¿ Apr 2, 2015 05:19 |
|
What's the scope of #define macros in C? I'm working on embedded stuff and see a lot of macros defined at the top of .c files, and just want to be sure those won't interfere with stuff I'm writing elsewhere in the project. I'm guessing that if the macros are defined in headers, then they'll be visible anywhere that includes them, but if they're in .c files they're restricted to that file? I'm not quite sure how the preprocessor parses #define. I suppose I could do a quick test...
|
# ¿ Apr 15, 2015 03:26 |
|
Suspicious Dish posted:When you use #include, the preprocessor literally just copy/pastes the included file into yours at that location. The Laplace Demon posted:Also note that if they occur above any #include directives, they're visible within the #included files too (and potentially re#defined to another value or #undefined entirely...) Thanks everyone!
|
# ¿ Apr 15, 2015 04:45 |
|
Soylent Pudding posted:Could someone elaborate on this please? I've done this (and seen my professors do this) for all my projects so far. I probably didn't explain it as eloquently or technically correct as stuff you'll find on , but it's bad practice and you shouldn't do it.
|
# ¿ Apr 16, 2015 06:11 |
|
Diametunim posted:Thanks for pointing me in the right direction. Why would you implement it this way vs a standard 1D array? If you're wondering why you can't do a statically allocated array, my assumption is that because you have two bucket count variables (an initial and current) you'll have to resize and rehash your table based on the load factor. You'll want to use dynamic allocation since you're not dealing with a compile-time constant for the array size. Edit: the copy constructor and assignment operator being disallowed gives me a feeling. I hope your instructor has you implement them at some point, since they're pretty critical to get right in a C++-based data structures course. They're also using a pre-C++11 method to disallow it (private member) as opposed to declaring it as = delete. That's not important but maybe you find that interesting Star War Sex Parrot fucked around with this message at 20:47 on Apr 26, 2015 |
# ¿ Apr 26, 2015 20:19 |
|
roomforthetuna posted:I've never seen anything that tries to *teach* debugging techniques. It's a really weird gap in CS curricula, interview tests, etc.
|
# ¿ May 2, 2015 20:38 |
|
|
# ¿ Apr 29, 2024 12:38 |
|
roomforthetuna posted:I'm sure it depends on the place. It seems like yours is the exceptional case, because internet questions about why poo poo's not working are clearly coming from people who haven't been taught any way to debug, but I suppose that might just be selection bias, maybe 95% of places teach that stuff and you just don't see any questions from those people because they can all solve their own "why doesn't this work" problems.
|
# ¿ May 2, 2015 21:22 |