473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Assigning the address returned from new to a reference

Can I write:

T i&=new T;

Thanks!!!

Nov 27 '05 #1
14 1291
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!
Nov 27 '05 #2
Protoman wrote:
Can I write:

T i&=new T;

Thanks!!!


I think you meant
T &i = new T ;

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

Nov 27 '05 #3
"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)
Nov 27 '05 #4
"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/
Nov 27 '05 #5
Protoman wrote:
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

Nov 27 '05 #6

Sandeep wrote:
Protoman wrote:
Can I write:

T i&=new T;

Thanks!!!


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!!!

Nov 27 '05 #7

Oliver S. wrote:
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!


What does "schrieb" mean? Are you German?

Nov 27 '05 #8

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Sandeep wrote:
Protoman wrote:
> Can I write:
>
> T i&=new T;
>
> Thanks!!!
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?


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
Nov 27 '05 #9
"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?
Nov 27 '05 #10
Jim Langston wrote:
"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.arco r-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?


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.
Nov 27 '05 #11

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Oliver S. wrote:
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!


What does "schrieb" mean?


wrote

-Mike
Nov 27 '05 #12
On the topic of pointers and references and whatnot, how to I overload
operator* to create a simple smart pointer?

Nov 27 '05 #13
Protoman wrote:
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;
}
};
Nov 27 '05 #14
n2xssvv g02gfr12930 wrote:
Protoman wrote:
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;
}
};


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
Nov 27 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Dave | last post by:
Hello all, Please consider the code below. It is representative of a problem I am having. foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not...
15
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
3
by: Ricky W. Hunt | last post by:
I came across something by accident; assigning one object to another (as in object1 = object2) allows me to "work on" object2 just like it was object1. For instance, in order to be able to...
12
by: ypjofficial | last post by:
Hello All, I need to return a vector from a function.The vector is of int and it can contain as many as 1000 elements.Earlier I was doing //function definition vector<intretIntVector() {...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
7
by: Daniel | last post by:
I am baffled. According to the C++ faq lite it is ok to use a reference as an lvalue, and the subscript operator returns a reference. However, when I run this program, it crashes! I will go set up...
3
by: Froefel | last post by:
Hi group I am creating a web application that uses a simple DAL as an ObjectDataSource. To retrieve data from the database, I use a DataReader object from which I then assign the various fields...
10
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
1
by: E11esar | last post by:
Hi there. I'm not sure if this is possible but here goes. I have an asp.net (c#) page and I want to populate a textbox (or whatever control is most suitable) with the text value returned by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.