"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:Zk*******************@twister.nyroc.rr.com...
"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:f9*****************@fe06.lga... "Oliver S." <Fo*******@gmx.net> wrote in message
news:43***********************@newsread2.arcor-online.net... Protoman schrieb:
Can I write:
T i&=new T;
Of course you can write this; but no conformat Compiler will
accept that. And I think its not the thing you wanted to ask;
I think you wanted to ask if something like ...
T &t = *new T();
... is legal. It is!
How would you delete it?
I don't believe you can delete a reference, can you? I don't believe
delete t
would compile (although I might test it and see)
delete t;
won't compile but
delete &t;
will. Not that I would write code like that myself. :P
--
Cy
http://home.rochester.rr.com/cyhome/
Interesting.
I have a certain class (CMap) that I wanted to make a vector out of, so I
did a simple
std::vector<CMap> Maps;
But it turns out that Maps.insert(Mymap) was giving me memory issues in some
cases (attempting to read or write memory it didn't "own") and stack
overflow in other cases. The most likely cause of this was a default copy
constructor.
Now, a copy constructor for CMap would be non trivial, as it contained
structures being loaded from a .dll using memory pointers and other things.
What I wound up doing was:
std::vector<CMap*> Maps;
Maps.insert(Mymap)
where MyMap was now a CMap* allocated with new.
Now I get the fun of having to use -> on CMaps[whatever].
Using the discussion of this thread, do you think it would good code to
change it to
std::vector<CMap&> Maps;
Maps.insert(MyMap);
Where again MyMap is a CMap*.
Then I dont' have to use -> but can use .
I would have to change my cleanup to
std::vector<CMap*>::iterator itend = World.Maps.end();
for ( std::vector<CMap*>::iterator it = World.Maps.begin(); it != itend;
++it )
delete &(*it);
As you say, you would never write code like this, and I'm thinking it may
not be a good idea. But it is a though. Any comments?