Hello all,
I am a C programmer learning C++. And I am confused with function Pass
by Pointer * or Pass by Reference &.
Function pass by pointer like:
void func(int * x)
And function pass by referencelike:
void func(int & x)
What's their main difference and be used in what circumstance?
Best regards,
Robert 10 40656
"Robert" <zh*******@gmai l.com> schrieb im Newsbeitrag
news:11******** *************@g 14g2000cwa.goog legroups.com... Hello all,
I am a C programmer learning C++. And I am confused with function Pass by Pointer * or Pass by Reference &.
Function pass by pointer like: void func(int * x) And function pass by referencelike: void func(int & x)
What's their main difference and be used in what circumstance?
Best regards, Robert
Pointer can be null, references can't. Pointers can be reassigned.
A reference is more comfortable to use. You can handle it like a normal
object. When changing a function from
void func(const AClass);
to
void func(const AClass&);
you do not need to rewrite your code.
Greetings Chris
Robert wrote: Hello all,
I am a C programmer learning C++. And I am confused with function Pass by Pointer * or Pass by Reference &.
Function pass by pointer like: void func(int * x) And function pass by referencelike: void func(int & x)
What's their main difference and be used in what circumstance?
Best regards, Robert
There is a lot of good information about this in this handy guide: http://www.parashift.com/c++-faq-lite/
--John Ratliff
Another problem is why reference cannot be able to represent a NULL
object?
Best regards,
Robert
One other difference between pointers and references is that if you use
dynamic_cast on an incorrect reference it willl throw a bad cast
exception. On the other hand, an incorrect dynamic cast on a pointer
will not throw a bad cast exception.
void function(Panda& q)
{
Panda &ip = dynamic_cast<ra ndomref&)(q);
}
void g()
{
try{
f(*(new Panda))
}
catch(bad_cast)
{
}
}
Robert ha scritto: Another problem is why reference cannot be able to represent a NULL object?
Best regards, Robert
References are intended to "point" to existing objects :
the function
void f(T &t)
{
[...]
}
is similar to
void f(T const *t)
{
assert(t != NULL);
[...]
}
Reference can't be null and its address can't be incremented.
Bye,
Giulio
Frank Chang schreef: One other difference between pointers and references is that if you use dynamic_cast on an incorrect reference it willl throw a bad cast exception. On the other hand, an incorrect dynamic cast on a pointer will not throw a bad cast exception.
No, it returns a NULL pointer. This can be useful:
void foo( Base* b )
{
if( Derived* d = dynamic_cast<De rived>(b) )
{
// do something with Derived
d->bar();
}
else
{
// User didn't have a Derived object, but we don't care why not
}
}
This is a quite common idiom. I usually leave out the comment and the
else block if there is no default action. The idea is that I don't
want to differentiate between a non-Derived Base object or no object
at all. I purely want to know if the caller has a Derived object.
HTH,
Michiel Salters
no it doesn't. On a pointer it returns null on references it throws
std::bad_cast
"Robert" <zh*******@gmai l.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. . Another problem is why reference cannot be able to represent a NULL object?
Best regards, Robert
A reference can't refer to a null object since its permanently bound to a
valid object. This is an important distinction (unlike a pointer which can
point to another object or even to a null object). A reference is a
permanent alias to a valid object.
In fact, the lifetime of any object may even be extended to satisfy this
object-is-valid rule.
"Robert" <zh*******@gmai l.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. . Another problem is why reference cannot be able to represent a NULL object?
Best regards, Robert
Define what a NULL object is.
Dan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: sam pal |
last post by:
I have the following programs and please clear my doubts. Passing the
value by reference is same as pointer by reference. Is this true?
What is the difference between display and display2?
How can I change display1 to value by reference if there is
difference?
void display(int &i){// Passing value by reference
|
by: Nelson |
last post by:
Hi,
Can pointers be passed by reference to functions? If so, how to do it?
Or is there any other way to change the pointers in the caller by the
called function?
Thanks,
Nelson
|
by: dover |
last post by:
There are two situations under which I am not sure which one to use, pointer
or reference:
Passing parameter into a function
A object is aggregated in another one.
Is there any general rules of thumb about this?
Many thanks!
|
by: Berk Birand |
last post by:
Hi,
I am just learning about the array/pointer duality in C/C++. I couldn't
help wondering, is there a way to pass an array by value? It seems like
the only way to do is to pass it by reference??
Thanks,
BB
|
by: DamonChong |
last post by:
Hi,
I am still struggling to master C++ and is trying to understand how to
achieve passing arguments using pointers. I got some questions that I
like to post to the experts here, hope you can help to clarify my
doubts. I'm using g++ version 3.3.4. I created 3 classes as below for
testing some concepts. The questions are written as comments in
Bclass.h file. Thank you for your time and tips!
------------runtime errors------------
| |
by: Scott McFadden |
last post by:
What is the proper way to pass pointers by reference from managed c++ calls
to native c++ calls?
In managed C++, I have a pointer to an array of structures, that I pass to a
native c++ method by reference:
//Managed c++
QueryResult* qr = NULL;
ExecuteQuery(1, "abc", qr); //invoke native c++ method passing qr by
reference
|
by: Scott |
last post by:
Hello,
I am having some difficulty with the following C++:
I have a declared a pointer to Foo like so:
Foo *pFoo;
Now, I have an API object that I try to use like so:
|
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);
|
by: happyvalley |
last post by:
Hi,
basically, the test function get a char pointer, and assigned a string
to it. then the string is passed back by the call-by-reference
mechanism. in test(), I reallocate some memory for the pointer, the
size is not fixed.
I remember, all new statement should be followed by a delete statement,
is there some memory leak here?
|
by: HSeganfredo |
last post by:
Folks,
I want to write a init string function that mallocs an area, fills it
with a char, sticks a NUL char in the last position and returns it to
the user.
So far I noticed that my implementation always works over a NEW memory
area, and not over the ORIGINAL pointer area, so the initial string
area is untouched, the pointer is not update to refer to the new
initiated area...
|
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...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |