|
quote:What happens if you do this? quote:Use of possibly unassigned field 'Baz' Which is completely true. I would have expected a NullReferenceError but that'll do.
|
# ? Sep 26, 2011 05:19 |
|
|
# ? Jun 1, 2024 09:04 |
|
thelightguy posted:I'm having a hard time figuring out where that might be in any way useful. Should declaring any value type initialize it to its default value? Why are structs a special case? Imagine if C++ behaved like C# value types except the compiler didn't bother you about uninitialized variables. That seems pretty in line with the rest of the language.
|
# ? Sep 26, 2011 05:29 |
|
Dicky B posted:Non c++ programmers talking about c++ This level of ignorance about C++ is yet another coding horror.
|
# ? Sep 26, 2011 07:45 |
|
References being invisible and impossible to manipulate separately to values is the real horror. I especially love the "safe" language fans arguing for uninitialised variables being a good thing.
|
# ? Sep 26, 2011 09:07 |
|
Sedro posted:Should declaring any value type initialize it to its default value? Yes (that default value being null in most cases.) I don't see any practical usage for an uninitialized variable.
|
# ? Sep 26, 2011 09:14 |
|
thelightguy posted:Yes (that default value being null in most cases.) I don't see any practical usage for an uninitialized variable. It's similar to how the compiler requires you to declare variables before using them, even when it's capable of inferring the type for you. There are uses for a partially-initialized struct - you might only ever use some of the fields, for example. And then the compiler checks for you and makes sure you never use one of the fields you haven't initialized.
|
# ? Sep 26, 2011 09:58 |
|
thelightguy posted:Yes (that default value being null in most cases.) I don't see any practical usage for an uninitialized variable. null is not a valid value of struct { int foo; }.
|
# ? Sep 26, 2011 10:17 |
|
This thread has gone off the loving wall. Next people are going to start flipping out when they learn that the destructor is called at the end of a variable's scope.
|
# ? Sep 26, 2011 13:03 |
Yeah. C++ is insanely complex. It takes lots of effort to learn all the details. When you know them, it's a really great language. The only true fault of C++ is that it's hard to learn. Let's rather discuss how retarded it is that "good old" C allows you to declare a function with an unknown argument list and then just call it with whatever parameters.
|
|
# ? Sep 26, 2011 13:19 |
|
nielsm posted:Yeah. C++ is insanely complex. It takes lots of effort to learn all the details. When you know them, it's a really great language. The only true fault of C++ is that it's hard to learn. Where other languages eschew complexity C++ embraces it with open arms and its tongue wagging. Being 'hard to learn is a symptom, along with the other symptoms of being hard to implement, hard to debug, etc.
|
# ? Sep 26, 2011 14:28 |
|
thelightguy posted:I'm having a hard time figuring out where that might be in any way useful. If you need access to an object in the finally block of a try, you have to declare it outside of the try, but you do not have to instantiate it outside the try.
|
# ? Sep 26, 2011 15:26 |
|
From 2 fast moving pages ago...Internet Janitor posted:I'm brushing up on C and networking stuff. The quality of example code online is great: It's networking code. It's probably source compatible with an embed chip running some clusterfuck of a Pre-ANSI C compiler and that's the only thing known to work on the 19 platforms it has to support.
|
# ? Sep 26, 2011 15:39 |
|
Munkeymon posted:If you need access to an object in the finally block of a try, you have to declare it outside of the try, but you do not have to instantiate it outside the try. code:
|
# ? Sep 26, 2011 16:07 |
|
Munkeymon posted:If you need access to an object in the finally block of a try, you have to declare it outside of the try, but you do not have to instantiate it outside the try. Or suppose you want a placeholder member variable at the object level that will be defined in constructor arguments: code:
There are lots of uses: code:
|
# ? Sep 26, 2011 16:09 |
|
baquerd posted:Or suppose you want a placeholder member variable at the object level that will be defined in constructor arguments: code:
quote:There are lots of uses: Well, aside from the fact that your goon.getChildField() is unlikely to work there (goon is MySuperClass not MyChildClass), if you have child specific cloning behaviour you should have a virtual clone method on the superclass which would allow: code:
|
# ? Sep 26, 2011 16:18 |
|
Those are all examples of pointer semantics anyhow and are not even addressing implicit calls of constructors
Mustach fucked around with this message at 16:32 on Sep 26, 2011 |
# ? Sep 26, 2011 16:27 |
|
Zombywuf posted:Well, aside from the fact that your goon.getChildField() is unlikely to work there (goon is MySuperClass not MyChildClass), if you have child specific cloning behaviour you should have a virtual clone method on the superclass which would allow:
|
# ? Sep 26, 2011 16:33 |
|
Orzo posted:You're addressing a totally different design concern, the example obviously illustrates the intended point. He's right though, sloppy code on my part and the Dog/Cat example illustrates this better.
|
# ? Sep 26, 2011 16:40 |
|
tef posted:Where other languages eschew complexity C++ embraces it with open arms and its tongue wagging. Being 'hard to learn is a symptom, along with the other symptoms of being hard to implement, hard to debug, etc. Here's a horror: Scrolling past your avatar in IE9 slows the browser down to a crawl.
|
# ? Sep 26, 2011 16:43 |
|
Zombywuf posted:References being invisible and impossible to manipulate separately to values is the real horror.
|
# ? Sep 26, 2011 16:55 |
|
Mustach posted:Those are all examples of pointer semantics anyhow and are not even addressing implicit calls of constructors He was asking why it would be useful to not have default constructors called when objects are declared. In C#, it's useful because there is no pointer-specific syntax. Oh, and code:
|
# ? Sep 26, 2011 17:00 |
|
code:
|
# ? Sep 26, 2011 17:10 |
|
baquerd posted:He's right though, sloppy code on my part and the Dog/Cat example illustrates this better. code:
Monkeymon posted:
|
# ? Sep 26, 2011 17:14 |
|
Mustach posted:That is a legitimate example (when SomeThing is a struct). The others are crap. I don't see any problem with the other examples - maybe I've lost track of exactly what everyone is arguing about? Some of you guys seem to be talking past each other.
|
# ? Sep 26, 2011 17:22 |
|
quote:It doesn't, because C and C++ are not Java or C#, variables are objects, not references to objects. Right, pretty much all of the strongly typed non-C++ world expects an uninitialized object to be a reference to a null or undefined object (edit: or actually be a null/undefined object), not to have it initialized by default for us. baquerd fucked around with this message at 17:38 on Sep 26, 2011 |
# ? Sep 26, 2011 17:31 |
|
baquerd posted:Right, pretty much all of the non-C++ world expects an uninitialized object to be a reference to a null or undefined object, not to have it initialized by default for us. The fact that you said uninitialised object rather than uninitialised variable demonstrates why the non C++ way is a horror.
|
# ? Sep 26, 2011 17:38 |
Munkeymon posted:If you need access to an object in the finally block of a try, you have to declare it outside of the try, but you do not have to instantiate it outside the try. Example, I have a handle value I need to close when a block is left, even if an exception is thrown: code:
baquerd posted:Or suppose you want a placeholder member variable at the object level that will be defined in constructor arguments: What you do in C++: code:
|
|
# ? Sep 26, 2011 17:39 |
|
nielsm posted:Pass a reference to a const Object in (faster pass by value, but still allows you to use a temporary unlike using a pointer type or a non-const reference), then initialise the butts field by its copy constructor from the argument. This is also the same syntax used to call superclass constructors. That's what I said, but by then the rest of the thread had forgotten we were talking about C++.
|
# ? Sep 26, 2011 17:42 |
|
thelightguy posted:I don't see any practical usage for an uninitialized variable.
|
# ? Sep 26, 2011 17:44 |
|
Zombywuf posted:The fact that you said uninitialised object rather than uninitialised variable demonstrates why the non C++ way is a horror. An object is conceptually different than a normal variable in that it can support member methods, variables, and objects. We're also talking about constructors here in particular... What don't you like about it? That it means I'm not thinking in terms of specific memory allocation and pointers?
|
# ? Sep 26, 2011 17:44 |
|
baquerd posted:An object is conceptually different than a normal variable in that it can support member methods, variables, and objects. We're also talking about constructors here in particular... object != variable A variable is a name used to refer to a value, an object is a value.
|
# ? Sep 26, 2011 17:47 |
|
Zombywuf posted:object != variable We're just operating under different terminology systems here. An object is a classifying description of a variable, a specific object's class is a variable type, a value is a value is a value.
|
# ? Sep 26, 2011 17:49 |
|
baquerd posted:What don't you like about it? That it means I'm not thinking in terms of specific memory allocation and pointers?
|
# ? Sep 26, 2011 17:53 |
|
Which is the bigger horror: C++, or the discussions people end up having about it?
|
# ? Sep 26, 2011 17:54 |
|
We can still rag on Java right? Ran into an issue where I wanted to be able to copy any object that was marked as copyable. No problem I figured, I seem to recall java had a cloneable interface, let's look it up to find out how to use it. Cloneable posted:Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface.
|
# ? Sep 26, 2011 17:55 |
|
Cloneable is fundamentally a problem because Object.clone() Doesn't specify deep or shallow copy semantics. edit: No, we are not having a "mutable state is a horror" rant.
|
# ? Sep 26, 2011 17:59 |
|
nielsm posted:Well, C++ doesn't have finally blocks, which can be annoying until you remember you're supposed to put your cleanup logic in your destructors. Then it isn't a problem all of a sudden. It's like garbage collection just better. ("C++ doesn't need garbage collection, because it doesn't produce garbage.") Yeah, I thought he was asking why it was a useful feature (to not call a default constructor) in C#. I know my C(++) is too rusty, so I haven't been talking about those languages. finally blocks are pretty boss, tho
|
# ? Sep 26, 2011 18:23 |
|
nielsm posted:("C++ doesn't need garbage collection, because it doesn't produce garbage.")
|
# ? Sep 26, 2011 18:29 |
|
RAII + boost smart pointers take care of most of it. Garbage collection isn't a panacea either.
|
# ? Sep 26, 2011 18:33 |
|
|
# ? Jun 1, 2024 09:04 |
|
baquerd posted:We're just operating under different terminology systems here. An object is a classifying description of a variable, a specific object's class is a variable type, a value is a value is a value.
|
# ? Sep 26, 2011 18:40 |