Quote:Original post by westond
Now that I know what the 0A 0D mean that will make it easier for text mode anyway. Im going to have to push this to binary I think since some of the objects will eventually have pointers to other types of objects. But, one step at a time.
Ouch. Be
VERY careful. You CANNOT just write a pointer value into a file and expect it to work when you read it back in. Why? Because even if you used arcane hacks (and believe me, they are arcane) to try to make every object be at the same place in memory as it was in the previous run (in addition to having them merely be "linked up" in the same way), you can't guarantee that all of the same chunks will be *available* to you this time. Instead, you need to write some kind of data that abstractly represents the way the objects pointed at each other before.
Seriously, just read the FAQ chapter (the link above).
Quote:I also dont seem to quite understand whats going on here
It's explained in the comment directly above:
Quote:
// While we are able to consume any leading whitespace (this will also
// skip past the newline left by a previous read if any), read up until a
// comma into text, and subsequently read into a number:
while (fin >> ws && getline(fin, text, ',') && fin >> id ) {
"while (fin >> ws", for example, attempts to do "fin >> ws". This operator actually will always return 'fin' back again. The skipping of any leading whitespace (this is the somewhat magical behaviour of reading into std::ws - it's the same manner of beast as std::endl for example) is done *as a side effect*. Then, since we're in a boolean context, 'fin' evaluates as true if and only if the stream is still "good"; i.e. the operation was successful. Similarly the other stream reads.
More info.Quote:
I understand the push back statement... thats actually a nice way to do it
It's standard (idiomatic) to add things to a vector that way.
Quote:except did you mean objects(id, text) since that is the reference name you gave the Object. (object was a bad class name.. but made sense to me)
No. Here, 'Object(id, text)' is a constructor call. It creates a temporary Object, and passes it to the push_back. push_back accepts the temporary by const reference (because it's a *const* reference, this will work with a temporary), and copies the Object into the vector. At the end of the statement, the temporary is out of scope, and its destructor (which happens in our case not to do anything) is called.
This is normal and idiomatic use of the language; it's how you get "literals" of object type instead of only having numeric/character/string literals.
Quote:
I like pointers much more than straight reference (right word?) that exist on the stack.
Values.
Quote:With a pointer I know it wont go out of scope and gives me more felxability...
The only flexibility you gain is the flexibility to mess up, add complexity to what you have to write, and add performance overhead. A stack value doesn't go out of scope until the end of the scope you declared it in; if you're having problems, think more about how you scope your variables. Anyway, you *want* to scope your variables as tightly as you possibly can; this is simply a generalization of the standard "globals are bad mm'kay" advice.
Quote:not to mention not having to mess with the copy constructor and destructor everytime I manipulate an object.
Um. I don't have any real idea what you're thinking here. If you're talking about calling a copy constructor or destructor, that's done *automatically by the language*. In fact, it's using extra dynamic allocation that produces extra burden for yourself: you become required to "mess with the destructor" by calling delete (or delete[] as necessary).
If you're talking about *writing* a copy constructor or destructor, then (a) whether or not you *need* one has absolutely, 100% nothing to do with whether you're going to create those objects on the stack or on the heap, and everything to do with dynamically allocated *members of the object*; and (b) the actual process of writing it is again totally independant. An object should not *itself, ever* care whether it is on the heap or on the stack (because it's pretty much impossible for it to *find out* anyway).
Quote:I think what I meant to do was
vector<Object> objvec;
*pobjvec=objvec;
That still doesn't make it a vector-of-pointers-to-Object (which you still don't need). It merely creates the vector-of-Object on the stack, and then copies it to the vector-of-Object that pobjvec is already pointing at. Note that this, in turn, is different from causing pobjvec to point at objvec (you would do that with "pobjvec = &objvec"). Doing things this way (a) requires that pobjvec *already* points at a valid vector-of-Object; (b) overwrites the contents of that vector-of-Object with the contents of objvec.
Quote:The reason I want to dynamically create the object is for the same reasons I want the vecter to be a pointer. In the end these vectors are only temporary holders of the objects. Once the objects are created in the larger project they will contain an x,y coordinate that will map them to a multidimensional array so I can activate objects by array arithmatic. Oh, and I should have done this Object *newobj = new Object;
That's not a reason, and to be honest you're making less and less sense. Please, I'm trying to make this easier for you. Why resist that?