Assigning the address returned from new to a reference

O

Oliver S.

Protoman said:
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!
 
J

Jim Langston

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!

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)
 
C

Cy Edmunds

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)

delete t;

won't compile but

delete &t;

will. Not that I would write code like that myself. :p
 
K

Kai-Uwe Bux

Protoman said:
Can I write:

T i&=new T;

You did. Therefore, you can.

However, what you wrote is not valid C++. More important, from the snippet
you provided, one cannot even guess what you want to accomplish. Could you
elaborate a little more?


Best

Kai-Uwe Bux
 
P

Protoman

Sandeep said:
I think you meant
T &i = new T ;

Why don't you write a small program and see what happens ?

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;? Thanks!!!
 
P

Protoman

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!

What does "schrieb" mean? Are you German?
 
M

Mike Wahler

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?

Frees the memory pointed to by &i
And when would I need to use such
a construct as T& i=*new T;?

Almost certainly never.


-Mike
 
J

Jim Langston

Cy Edmunds said:
delete t;

won't compile but

delete &t;

will. Not that I would write code like that myself. :p

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?
 
M

Mark P

Jim said:
delete t;

won't compile but

delete &t;

will. Not that I would write code like that myself. :p


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?

You can't make STL containers of references. A container of pointers is
the way to deal with such a thing. If you find the interface unwieldy
(though really, what's an occasional -> between friends?) then you can
wrap the container in another class and design your own interface to
automate these sorts of tasks.
 
P

Protoman

On the topic of pointers and references and whatnot, how to I overload
operator* to create a simple smart pointer?
 
N

n2xssvv g02gfr12930

Protoman said:
Can I write:

T i&=new T;

Thanks!!!

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;
}
};
 
N

n2xssvv g02gfr12930

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;
}
};

Minor correction, the following code could work but it's not advisable.

T i;
i &= new T;

see other replies for what you probably required.

JB
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top