473,385 Members | 1,707 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,385 software developers and data experts.

q: Pointer vs Reference question

Hi. If i have

Class Object {
}

void foo(Object* o)
{
}

void bar(Object& o)
{
}

int main()
{
Object o;
foo(&o);
bar(o);
return 0;
}

I know that in foo, the contents of o are not copied as main just
passes a pointer to the function foo, but my question is, in bar, is
the object o copied? or is just a reference to it passed to the
function? thanks!

Oliver

Jul 23 '05 #1
3 1535
* laniik:

Class Object {
}
NP: missing semicolon.

void foo(Object* o) {}
void bar(Object& o) {}

int main()
{
Object o;
foo(&o);
bar(o);
return 0;
}

I know that in foo, the contents of o are not copied as main just
passes a pointer to the function foo, but my question is, in bar, is
the object o copied? or is just a reference to it passed to the
function? thanks!


Just a reference, no object copying.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
thanks!

what a great sig also

Jul 23 '05 #3

"laniik" <la****@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi. If i have

Class Object {
}

void foo(Object* o)
{
}

void bar(Object& o)
{
}

int main()
{
Object o;
foo(&o);
bar(o);
return 0;
}

I know that in foo, the contents of o are not copied as main just
passes a pointer to the function foo, but my question is, in bar, is
the object o copied? or is just a reference to it passed to the
function? thanks!


The function bar(...) doesn't use a copy either. But there is more happening
here than meets the eye.

The Object& o parameter in bar(...) can't be unbound, not true for a
pointer. This does not mean you can't change the reference's contents. The
reference can't be un-initialized. Hence, in certain cases, a reference can
even extend the lifetime of an object (that doesn't apply here).

Jul 23 '05 #4

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

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
6
by: amparikh | last post by:
I know this is something fundamental and I ought to have known it, but somehow this seems to be confusing me a lot. Fundamentally, rvalues and/or temporaries can be bound only to constant...
110
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...
2
by: bilaribilari | last post by:
Hi, I came across a function which looks like this: void func ( Object *& param1 ) { .... } Can someone please explain what does *& mean? Where can I get a good understanding of 'pointer...
2
by: Edward Diener | last post by:
How does one specify in a component that a property is a pointer to another component ? How is this different from a property that is actually an embedded component ? Finally how is one notified in...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
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...
2
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
41
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.