|
Will Vale
|
 |
« Reply #45 on: March 10, 2012, 10:15:04 PM » |
|
and process it by reference.
Thanks, that's what I was curious about. I agree that owning data by value is desirable, although it does imply (in C++) a degree of dependency (#include rather than forward-declare) which you could otherwise avoid. But maybe that's a reasonable price to pay for the degree of transparency and simplicity you get. Will
|
|
|
|
|
Logged
|
|
|
|
|
BlueSweatshirt
|
 |
« Reply #46 on: March 10, 2012, 10:25:05 PM » |
|
What do you guys exactly mean by "owning data by value" as opposed to by reference?
|
|
|
|
|
Logged
|
|
|
|
|
Sakar
|
 |
« Reply #47 on: March 10, 2012, 10:45:37 PM » |
|
What do you guys exactly mean by "owning data by value" as opposed to by reference?
This is probably a bad explanation, but here it goes: Owning data by value means that something owns the actual value of the data, like the value a pointer points to. By reference means something holds a reference to it, like a pointer points to memory, but doesn't actual have it. Basically its like the difference between pointers and normal variables 
|
|
|
|
|
Logged
|
|
|
|
|
Will Vale
|
 |
« Reply #48 on: March 10, 2012, 11:33:26 PM » |
|
That's about right. Maybe a bit of code will help too: struct Bullet { float x, y; };
// Bullets by reference - we point to (refer to) bullets in memory somewhere. std::vector<Bullet*> bullet_references;
// Bullets by value - we have the complete bullet states (values) right here. std::vector<Bullet> bullet_values;
|
|
|
|
|
Logged
|
|
|
|
|
BlueSweatshirt
|
 |
« Reply #49 on: March 10, 2012, 11:45:30 PM » |
|
Oh.  I was under the impression you guys were talking about something way different.
|
|
|
|
|
Logged
|
|
|
|
|
Will Vale
|
 |
« Reply #50 on: March 11, 2012, 12:10:02 AM » |
|
Unfortunately C++ is mired in jargon. For instance, the appallingly-named RAII (Resource Acquisition Is Initialisation) means more-or-less "acquire things you need in constructors, release them again in destructors". I can't think of a more opaque name for it than RAII  class File { public: File( const char *name ) : fp(fopen(name, "rb")) {} ~File() { fclose(fp); }
private: FILE *fp; };
|
|
|
|
|
Logged
|
|
|
|
|
paste
|
 |
« Reply #51 on: March 11, 2012, 07:19:31 AM » |
|
Owning data by value means that something owns the actual value of the data, like the value a pointer points to. By reference means something holds a reference to it, like a pointer points to memory, but doesn't actual have it.
Basically its like the difference between pointers and normal variables
What's the benefit of it? Less memory leaks and easier readability I'm guessing? Why use pointers at all then?
|
|
|
|
|
Logged
|
|
|
|
|
PompiPompi
|
 |
« Reply #52 on: March 11, 2012, 08:41:02 AM » |
|
You "have" to use pointers. If you want an array that change it's size at run time, you "have" to use pointers. If you want a linked list, you "have" to use pointers. The thing is, you don't have to use '*' in many cases. But you can use STL(Standard Template Library) which implement many of the things requiring pointers, for you. So when you want a linked list, you just do std::list<T>, instead of some struct Node { Node * Next;} Some mentioned boost, I actually don't like to use boost if I don't have to. I think smart pointers should be used mostly when seperate your program into modules. So basically, you can avoid using '*' in many cases, but behind the scenes, when you use std::vector and std::list, there will be a pointer hidden in their implementation somewhere.
|
|
|
|
|
Logged
|
 Kickstarter? no no no... it's Kicksucker...
|
|
|
|
Skomakar'n
|
 |
« Reply #53 on: March 11, 2012, 09:00:46 AM » |
|
sometimes obscure C++ knowledge. (E.g., "Explain what the 'mutable' keyword does.") Mutable isn't obscure. Volatile is. @Average Software: I've been reading that Stroustrup book on and off lately. What book would you recommend for C? I'm guessing Kernighan and Ritchie's? What do you mean by "a proud language lawyer"? Why aren't I asking you this stuff on IRC? Because if you ask it in this thread, the eventual reply will remain here for anyone to see and benefit from, which is a mighty fine thing.
|
|
|
|
|
Logged
|
|
|
|
|
Klaim
|
 |
« Reply #54 on: March 11, 2012, 09:05:27 AM » |
|
Unfortunately C++ is mired in jargon. For instance, the appallingly-named RAII (Resource Acquisition Is Initialisation) means more-or-less "acquire things you need in constructors, release them again in destructors". I can't think of a more opaque name for it than RAII  RAII name comes from Stroustup that himself says that he's really not good at "marketing" and should have asked someone else for another name. The problem is that the term is already widely used in C++-specific circles... It would be nice to have a better name, but oh well...
|
|
|
|
|
Logged
|
|
|
|
|
rivon
|
 |
« Reply #55 on: March 11, 2012, 01:09:52 PM » |
|
You "have" to use pointers. If you want an array that change it's size at run time, you "have" to use pointers. If you want a linked list, you "have" to use pointers. The thing is, you don't have to use '*' in many cases. But you can use STL(Standard Template Library) which implement many of the things requiring pointers, for you. So when you want a linked list, you just do std::list<T>, instead of some struct Node { Node * Next;} Some mentioned boost, I actually don't like to use boost if I don't have to. I think smart pointers should be used mostly when seperate your program into modules. So basically, you can avoid using '*' in many cases, but behind the scenes, when you use std::vector and std::list, there will be a pointer hidden in their implementation somewhere.
You also need pointers when you plan to use a lot of data as stack memory is limited and therefore you need to allocate memory for the data on heap. sometimes obscure C++ knowledge. (E.g., "Explain what the 'mutable' keyword does.") Mutable isn't obscure. Volatile is. I don't know either and I never ever needed it 
|
|
|
|
|
Logged
|
|
|
|
|
Average Software
|
 |
« Reply #56 on: March 11, 2012, 02:09:51 PM » |
|
sometimes obscure C++ knowledge. (E.g., "Explain what the 'mutable' keyword does.") Mutable isn't obscure. Volatile is. I don't know either and I never ever needed it  I use mutable quite often, and there are lots of neat tricks you can do with volatile overloading. I once wrote a queue class that was mutex locked for concurrent access if you declared it to be volatile. Outside of embedded programming and some concurrency situations though, volatile probably doesn't see a lot of use. Truly obscure, at least for me, were bitfields. struct Bitfield { unsigned bit_1 : 1; unsigned bit_2 : 1; unsigned two_bits: 2; }; I'm not going to explain that code, look it up if you're interested.
|
|
|
|
|
Logged
|
|
|
|
|
Moczan
|
 |
« Reply #57 on: March 12, 2012, 01:58:42 AM » |
|
Wow, I'm not really a C++ expert, but I always thought that bits being fundamental to programming, using bitfields and bitwise operators was common.
|
|
|
|
|
Logged
|
|
|
|
|
peous
|
 |
« Reply #58 on: March 12, 2012, 04:41:46 AM » |
|
No, usually it's not useful to know bits when using C++ About volatile, the problem is that implementation can be compiler dependent, and optimization-level dependant, so I usually don't use it
|
|
|
|
|
Logged
|
|
|
|
|
rivon
|
 |
« Reply #59 on: March 12, 2012, 05:20:09 AM » |
|
You don't generally work directly with bits unless you're doing encryption or compression or something like that.
|
|
|
|
|
Logged
|
|
|
|
|