P
Protoman
Can I write:
T i&=new T;
Thanks!!!
T i&=new T;
Thanks!!!
Protoman said:Can I write:
T i&=new T;
Protoman said:Can I write:
T i&=new T;
Thanks!!!
Oliver S. said: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!
Jim Langston said: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)
Protoman said:Can I write:
T i&=new T;
Sandeep said:I think you meant
T &i = new T ;
Why don't you write a small program and see what happens ?
Oliver said: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!
Protoman said:Here; it works.
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int& i=*new int(5);
cout << i << endl;
delete &i;
system("PAUSE");
return EXIT_SUCCESS;
}
Now, what exactly does delete &i do?
And when would I need to use such
a construct as T& i=*new T;?
Cy Edmunds said:delete t;
won't compile but
delete &t;
will. Not that I would write code like that myself.
Jim said:delete t;
won't compile but
delete &t;
will. Not that I would write code like that myself.
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?
Protoman said:Can I write:
T i&=new T;
Thanks!!!
n2xssvv said:Yes, if T has the operator &=(const T *) implemented. Unfortunately as
already pointed out, the code as it is will not be able to reliably
delete the memory allocated for T. The incomplete example class below
should demonstrate how it could be done.
class Example
{
private:
int val;
public:
Example &operator &=(const Example *inp)
{
val &= inp->val;
return *this;
}
};
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.