|
|
Orchid XP v8 <voi### [at] devnull> wrote:
> Rule 2 is the one that puzzles me. At least in C, *anything* larger than
> a machine register is a pointer. So "avoid pointers" is pretty much
> impossible as soon as you want to touch anything larger than 32 bits. No
> strings, no arrays, no structs, nothing. Unless C++ radically changes
> the rules in this respect, I'm not sure I see a way round that.
std::string someText(1000, ' ');
someText += "Hello";
someText.at(10) = 'A';
std::vector<int> anArray(1000);
anArray.at(10) = 5;
std::vector<std::string> anArrayOfStrings(1000);
anArrayOfStrings.at(10) = "Hello";
std::cout << someText << "\n" << anArrayOfString.at(10) << "\n";
Lots of dynamically allocated data, no pointers anywhere.
> Regarding rule 4... does this mean C++ at least manages to provide some
> standard containers *other* than arrays?
Dynamically allocated vectors, strings, doubly-linked lists, binary trees
(as sets, multisets, maps and multimaps) and double-ended queues. None of
them requiring any pointers or 'new' to use. (The upcoming C++ standard
will also include hashmaps as a new data container.)
> (And that you can *query the size* of an array?)
Getting the size of a vector, string, deque and binary tree is O(1).
(For doubly-linked lists it's not guaranteed.)
> > These rules should be broken only when you get very fluent
> > with the language and know what you are doing.
> But you can 100% guarantee that any code you read that was written by
> somebody else will be full of such exceptions. ;-)
Don't make the same mistakes as others.
--
- Warp
Post a reply to this message
|
|