473,795 Members | 3,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reference vs. pointer

I get that these two are different

int* get()
{
static int m;
return &m;
}

int& get()
{
static int m;
return m;
}

int& get()
{
static int m;
int* p=&m;
return p; // should it be *p or p?
}
but can there be a case when pointers may be confused with references?
The reason I am asking is because the pointer contains the address of
an object so its value is a reference.

Mar 31 '06
13 2683

al*****@gmail.c om wrote:
Tomás wrote:
int& = *new int;


Typo:
int& i = *new int;
-Tomás


Interesting. Let me make an attempt to see what the code does.
i is of type int&
*new int = *(new int) = *(pointer to memory allocated to store an
integer) = **(memory allocated for int)

thus if i=*new int; then i should be of type **?


No, you've gone the wrong way in your last step.

*new int => *(new int)
*(new int) => *(pointer to an int)

Now what happens when you apply * the dereference operator to a
pointer? You get the object pointed to by that pointer, so

*(poiner to an int) => (the int)

So in the code

int& i = *new int;

i is a reference to the newly created int.

Gavin Deane

Apr 1 '06 #11
Gavin Deane wrote:
al*****@gmail.c om wrote:
Tomás wrote:
> int& = *new int;

Typo:
int& i = *new int;
-Tomás


Interesting. Let me make an attempt to see what the code does.
i is of type int&
*new int = *(new int) = *(pointer to memory allocated to store an
integer) = **(memory allocated for int)

thus if i=*new int; then i should be of type **?


No, you've gone the wrong way in your last step.

*new int => *(new int)
*(new int) => *(pointer to an int)

Now what happens when you apply * the dereference operator to a
pointer? You get the object pointed to by that pointer, so

*(poiner to an int) => (the int)

So in the code

int& i = *new int;

i is a reference to the newly created int.

Gavin Deane


Ah yes, you are right. Blunder! And to think I'm going to work at a
financial company this summer!

Apr 1 '06 #12
al*****@gmail.c om wrote:
Jakob Bieling wrote:
al*****@gmail.c om wrote:
in*****@gmail.c om wrote:
al*****@gmail.c om wrote:
> int& get()
> {
> static int m;
> int* p=&m;
> return p; // should it be *p or p?

It needs to be *p. Otherwise it will not compile.

thats true, but WHY?


Because they are different types.


Is there a difference between a C reference and a C++ reference? By C
reference I mean
int& a;
type declarations.


As far as I know, C does not have any references .. ?
--
jb

(reply address in rot13, unscramble first)
Apr 1 '06 #13
No, you've gone the wrong way in your last step.

*new int => *(new int)
*(new int) => *(pointer to an int)

Now what happens when you apply * the dereference operator to a
pointer? You get the object pointed to by that pointer, so

*(poiner to an int) => (the int)

So in the code

int& i = *new int;

i is a reference to the newly created int.

Gavin Deane


Here's how I like to think of it.

int &i = *new int;

First of all, that's the same as:

int &i = * (new int);

The thing on the left hand side of the assignment is of the type "int". The
thing on the left hand side of the assignment must an l-value of type "int".
Let's break it down into expressions. First let's look at the following
expression:

new int

The type of this expression is "int *", as "new" returns a pointer to the
object that's created. If you stick an asterisk behind an expression of type
"int*", you're left with an expression of type "int". Therefore, the
following expression is of the type "int":

*new int

Now we can bind a reference to it.

int &i = *new int;

delete &i;

-Tomás
Apr 1 '06 #14

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

Similar topics

110
9967
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
9
3004
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody tell me which one will be more efficient and why? as far as i know both must be same, because i read somewhere that internally
18
3541
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
12
5405
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
51
4477
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
2402
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference (or better a const reference). for eg, const PointRange* points = cc.points(ptAligned);
11
4243
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
29
3656
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
41
3689
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what pointer and reference are and how they relate to each other.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.