|
What looping constructs have you tried? It's better that you try and think it through instead of just coming to this thread first.
|
# ? Dec 16, 2009 04:08 |
|
|
# ? Jun 4, 2024 19:09 |
|
Dijkstracula posted:What looping constructs have you tried? It's better that you try and think it through instead of just coming to this thread first. thank you for this response... I just tried it with a while loop (in place of the if, and replace the else if with just an if)) and got the result I was looking for. ....now to try to figure out how to code in the error reporting for months that have 31 versus 30 days. Fatty_McLumpkin fucked around with this message at 04:43 on Dec 16, 2009 |
# ? Dec 16, 2009 04:31 |
|
I have a third-party library that, when it does its thing, writes to stdout. I want it to not do this. It seems like I might be able to use freopen somehow, but I don't know how to redirect stdout to nowhere before I execute the library function (I'm on windows, so no /dev/null) and back to what it was afterward, or even if this is the way to go. edit: there actually was an option to turn this off squirreled away somewhere, but I'm still curious about whether this is possible raminasi fucked around with this message at 23:09 on Dec 18, 2009 |
# ? Dec 18, 2009 22:25 |
|
GrumpyDoctor posted:I have a third-party library that, when it does its thing, writes to stdout. I want it to not do this. It seems like I might be able to use freopen somehow, but I don't know how to redirect stdout to nowhere before I execute the library function (I'm on windows, so no /dev/null) and back to what it was afterward, or even if this is the way to go. freopen("\\\\.\\Device\\Null", "w", stdout) might work.
|
# ? Dec 19, 2009 05:32 |
|
GrumpyDoctor posted:I have a third-party library that, when it does its thing, writes to stdout. I want it to not do this. It seems like I might be able to use freopen somehow, but I don't know how to redirect stdout to nowhere before I execute the library function (I'm on windows, so no /dev/null) and back to what it was afterward, or even if this is the way to go. You can redirect to nul! its the same thing!
|
# ? Dec 19, 2009 07:56 |
|
If I copy an object of a class without an explicitly-defined copy constructor but with a superclass with one, what gets called?
|
# ? Dec 20, 2009 23:35 |
|
GrumpyDoctor posted:If I copy an object of a class without an explicitly-defined copy constructor but with a superclass with one, what gets called? The default copy constructor calls the base class copy constructors and member copy constructors.
|
# ? Dec 20, 2009 23:59 |
|
Can somebody drop some words of wisdom upon const references and returning them in a function? Specifically what I want to do is return a reference to my internal object but as const to make sure it is not modified once it is returned. Should I just return a const pointer instead? I'm sorry my C++ is rusty and I would like to hear from professionals on this matter, and what sort of pitfals there could be to this.
|
# ? Dec 21, 2009 05:54 |
|
RussianManiac posted:Can somebody drop some words of wisdom upon const references and returning them in a function? I can't think of a reason to return a pointer-to-const over a const reference for an accessor.
|
# ? Dec 21, 2009 08:09 |
|
floWenoL posted:I can't think of a reason to return a pointer-to-const over a const reference for an accessor. explain please.
|
# ? Dec 21, 2009 08:20 |
|
floWenoL posted:I can't think of a reason to return a pointer-to-const over a const reference for an accessor. That's not true at all. If your member is dynamically allocated you better use a pointer instead, on account of the whole "dereferencing a NULL is kinda bad". You could make an argument that for members that are allocated at construction time dereferencing the pointer in a reference would be safe(ish). But that's asking for trouble in the long run. However, if the member is not a pointer, a reference is always better. Simple rule of thumb: A reference -> equivalent pointer cast is always fine. If you ever have to do pointer -> reference then something has generally gone wrong. Because of that, always use references where possible, since you can always bail to a pointer if something requires it anyways. Edit: I am talking about high-level organizational structures here. Operators and small operations are a different matter. Aramis fucked around with this message at 08:28 on Dec 21, 2009 |
# ? Dec 21, 2009 08:22 |
|
Aramis posted:That's not true at all. If your member is dynamically allocated you better use a pointer instead, on account of the whole "dereferencing a NULL is kinda bad". You could make an argument that for members that are allocated at construction time dereferencing the pointer in a reference would be safe(ish). But that's asking for trouble in the long run. code:
You can also use boost::optional instead of a pointer to achieve the same behavior. Paniolo fucked around with this message at 09:29 on Dec 21, 2009 |
# ? Dec 21, 2009 09:10 |
|
RussianManiac posted:explain please. If you are guaranteed to have an object and don't want to allow the returning of null, there is no reason to return a pointer and I'd go so far as to say it would be worse to do so. By giving the user of the function a pointer you are not implicitly communicating the fact that you will always retrieve a valid object. The reference gives the user more information. Aramis posted:That's not true at all. If your member is dynamically allocated you better use a pointer instead, on account of the whole "dereferencing a NULL is kinda bad". You could make an argument that for members that are allocated at construction time dereferencing the pointer in a reference would be safe(ish). But that's asking for trouble in the long run. What are you talking about? Whether or not it was dynamically allocated and/or referenced internally as a pointer is an implementation detail. If the function is supposed to always yield a valid object then a reference or a copy is going to be better. The only time it ever makes sense to return a pointer is if, by design, the user of the function should expect a null pointer. The poster of the question said nothing of the sort. Aramis posted:Simple rule of thumb: A reference -> equivalent pointer cast is always fine. If you ever have to do pointer -> reference then something has generally gone wrong.
|
# ? Dec 21, 2009 09:21 |
|
Paniolo posted:
The fact that it's feasible changes nothing to the issue at hand. C++ is riddled with ways to do things that work but are totally inadvisable until you are painted in a corner. const_cast is a great (possibly best) example of this. In your example, if the pointer is null, you still de-reference it (in a release build). It's way, way safer to address this at the call point of the function. code:
edit: That Turkey Story posted:I'm sorry, but that's total poo poo. There is nothing wrong with dereferencing a pointer and forming a reference unless you have a null pointer, which you either know by design or by an explicit check. Something has not "generally gone wrong" when you are correctly working with dynamically allocated memory or simply working through indirection and you need to form a reference from a pointer. Remember the context, we are talking about an accessor here. Which means that there is no possibility of a fallback, unless you throw an exception you'll need to dereference something. Aramis fucked around with this message at 09:35 on Dec 21, 2009 |
# ? Dec 21, 2009 09:31 |
|
Aramis posted:The fact that it's feasible changes nothing to the issue at hand. C++ is riddled with ways to do things that work but are totally inadvisable until you are painted in a corner. const_cast is a great (possibly best) example of this. In your example, if the pointer is null, you still de-reference it (in a release build). It's way, way safer to address this at the call point of the function. As an example, if you write a smart pointer and are making operator* you generally make it assert that you have a pointer to a valid object since dereferencing a smart pointer that is null, much like dereferencing a regular pointer that is null, is undefined behavior. The same applies to any other function. Aramis posted:I cannot stress this point enough, and I get pissed at junior programmers on a regular basis for it: possible/works != license to use.
|
# ? Dec 21, 2009 09:40 |
|
Aramis posted:It's way, way safer to address this at the call point of the function. So, in the fallback case of that if you wrote after the callpoint, what exactly do you propose gets done? Call assert_not_reached? Throw an exception? Do nothing, and violate whatever contract you were hoping to provide to code downstream? Why is it any better that these things are done at the callpoint, where they'll presumably have to be retyped every time the accessor is used, with all the potential for bugs that entails, rather than doing them once inside the function itself? Seriously, a reference return is the way to write functions that guarantee they always return valid memory. It is perfectly valid to write such functions even in systems with dynamic allocation because a dynamic allocation failure is possibly the single most obvious example of an exceptional condition, which is what exceptions are for. You shouldn't clutter up your contracts with extremely unlikely conditions that require 99% of your callers to duplicate the same boilerplate code at every callsite; just write the contract to the common case, and signal failure with an exception, which the 1% of callers who are capable of handling such a situation can easily catch.
|
# ? Dec 21, 2009 09:44 |
|
I love you guys so much. Thanks.
|
# ? Dec 21, 2009 09:52 |
|
Aramis posted:Remember the context, we are talking about an accessor here. Which means that there is no possibility of a fallback, unless you throw an exception you'll need to dereference something. If that's the case then your object is in an invalid state, meaning that either the class was designed poorly or the operation that put the object into an invalid state should have alerted users at a previous point (either by exception, or some kind of error flag if you are avoiding exceptions, etc.). By relaxing the requirements of the function to return a pointer instead of a reference for the paranoia that the object might be in an invalid state rather than correctly handling the problem at its source you only succeed at complicating both the function and all of the users of the class, not to mention that you are needlessly exposing implementation details and missing a much more fundamental problem with the design of your class.
|
# ? Dec 21, 2009 09:55 |
|
Aramis posted:The fact that it's feasible changes nothing to the issue at hand. C++ is riddled with ways to do things that work but are totally inadvisable until you are painted in a corner. const_cast is a great (possibly best) example of this. In your example, if the pointer is null, you still de-reference it (in a release build). It's way, way safer to address this at the call point of the function. Way to completely ignore the part where I said "or throw an exception", which is strange because you even quoted it. Aramis posted:Remember the context, we are talking about an accessor here. Which means that there is no possibility of a fallback, unless you throw an exception you'll need to dereference something. This is exactly what exceptions are for. You're basically saying, "Sure we could use the built-in language construct design to handle this situation, or we could just pass the responsibility onto the callers and force them to add boilerplate error-checking code at every call site." I would hate to work with any API you designed.
|
# ? Dec 21, 2009 10:03 |
|
I usually write my member accessors following this patterncode:
code:
|
# ? Dec 21, 2009 18:20 |
|
The1ManMoshPit posted:code paranoia You, sir, are a genius. And kudos for first time I've ever seen a 'this == NULL' comparison done (which is actually kind of surprising).
|
# ? Dec 21, 2009 18:37 |
|
That's almost perfect, but I think you should change all instances of "if ( var == NULL )" to "if ( NULL == var )". You're not a real coder until you're afraid of the drop shadow effects of your own windows.
|
# ? Dec 22, 2009 11:00 |
|
Sorry for asking this, but inheritance never fails to confuse the hell out of me. I've got a base Object class, from which I derive three other classes. I store the instances of these classes in a linked list, where each node contains anObject* pointer, so that it can take any of the three derived classes. But when I call a function, say Update(), on the pointers it calls the function Update() as defined in Object and not in any of the derived classes. I could swear that this shouldn't happen - I could swear that I've tried something similar elsewhere in the code (when storing States) that worked, but I can't see a difference between the two. I never get the nuances of inheritance/polymorphism/whatchamacallit. I've got code, but all the relevant bits are scattered here and there so I figured this would be a clearer explanation.
|
# ? Dec 22, 2009 21:49 |
|
Is Update() declared virtual in the base Object class?
|
# ? Dec 22, 2009 21:52 |
|
Is Update() declared as virtual in Object?
|
# ? Dec 22, 2009 21:53 |
|
I am slowly teaching myself the basics of C++, but for some reason I just cannot grasp the concept of certain pointer uses. I know what a pointer is and how to assign and access one, but directing input into an array of pointers is throwing me off.code:
|
# ? Dec 22, 2009 22:30 |
|
The1ManMoshPit posted:Is Update() declared virtual in the base Object class? Mustach posted:Is Update() declared as virtual in Object? Yep. code:
|
# ? Dec 22, 2009 22:36 |
|
Subway Ninja posted:your program boils down to std::cin >> "";. This has two problems: a) istream::operator>>(char *) reads some number of characters and writes them to the supplied address. The number of characters can be given an upper bound, but you didn't so this would be a buffer overrun. In any case, its a lovely misfeature akin to gets(3) -- don't use it. b) "" is a string literal, a pointer to a single NUL. Writing to the address if a string literal is undefined behaviour, on modern implementations your program will segfault at this point. in other words, use std::string already.
|
# ? Dec 22, 2009 22:42 |
|
Thanks for the quick reply. I'm working through an online tutorial at MSDN so I hadn't gotten to std::string yet.
|
# ? Dec 22, 2009 22:51 |
|
Staggy posted:Yep. Without more code it's going to be pretty hard to know exactly what your problem is. Are you sure the objects you're inserting into your list are actually instances of the derived classes and not the base class?
|
# ? Dec 22, 2009 23:07 |
|
Well the linked list is made up of these lovely structures, so I would imagine this is what you're talking about.code:
code:
code:
I think this is the crux of the problem. But I was under the impression that you could store derived classes in this way and still have them remain the derived class. Or store them at all, as I'm currently having trouble with.
|
# ? Dec 23, 2009 00:14 |
|
http://en.wikipedia.org/wiki/Object_slicing Make ObjectManager::AddObj take a pointer to an Object instead and it'll work
|
# ? Dec 23, 2009 00:36 |
|
Plorkyeran posted:http://en.wikipedia.org/wiki/Object_slicing Thank you! This was exactly it! I've got the whole thing fixed now, and I'm aware of the issue in the future. Thanks again.
|
# ? Dec 23, 2009 01:04 |
|
Staggy posted:Thank you! This was exactly it! I've got the whole thing fixed now, and I'm aware of the issue in the future. Thanks again. If you never want to create an Object directly, only via its subclasses, then you can get the compiler to catch this kind of mistake for you. Just make at least one method on Object "pure virtual", like so: code:
|
# ? Dec 23, 2009 01:26 |
|
I was being a little careless and something like this slipped into my code and compiled without warnings (and caused a nasty bug since the human-intuitive way to parse this is wrong):code:
|
# ? Dec 23, 2009 08:48 |
|
That's perfectly legal and there's even semi-valid reasons to want to do it, such as Duff's device. Switch requires that the first case statement (or default) comes before any expressions in switch's body, but nothing says that subsequent case statements cannot be inside other statements.
|
# ? Dec 23, 2009 09:01 |
|
If I have the Diamond Inheritance going on in my C++ project, then if all functions in the super class are virtual then i don't have any problems right? What if that super class also has a protected member variable?
|
# ? Dec 24, 2009 00:12 |
|
itym virtual super class
|
# ? Dec 24, 2009 00:40 |
|
Vanadium posted:itym virtual super class It is a super class that has virtual methods in it, yes, but also has a member variable.
|
# ? Dec 24, 2009 00:44 |
|
|
# ? Jun 4, 2024 19:09 |
|
RussianManiac posted:It is a super class that has virtual methods in it, yes, but also has a member variable. A virtual superclass isn't the same thing as a superclass with virtual member functions. Edit, to be at least somewhat helpful: When doing inheritance, besides just saying Foo: public Bar, you can also say Foo: public virtual Bar. You need the 'virtual' to put things to be a diamond, and not to have 2 copies instead. I'll leave the details to you or more helpful posters.
|
# ? Dec 24, 2009 00:57 |