Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Help with cpp pointers and references and const
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
alamahant
Advocate
Advocate


Joined: 23 Mar 2019
Posts: 3919

PostPosted: Tue Dec 03, 2024 3:50 pm    Post subject: Help with cpp pointers and references and const Reply with quote

Hello Guys,
For the last two months I have been playing around a bit with cpp and although I understand a bit the meaning of pointers and references and constants when I see some real world code I get overwhelmed by the cataclysm of them.
It is as if nobody hardly ever uses plain variables.
Most methods use const references for their parameters.
I feel extremely confused.
How can one get the grip of these things?
How did you do it?
Thanks for bearing with my ignorance and any advise would be super... :)
_________________
:)
Back to top
View user's profile Send private message
logrusx
Advocate
Advocate


Joined: 22 Feb 2018
Posts: 2475

PostPosted: Tue Dec 03, 2024 4:47 pm    Post subject: Re: Help with cpp pointers and references and const Reply with quote

alamahant wrote:
Hello Guys,
For the last two months I have been playing around a bit with cpp and although I understand a bit the meaning of pointers and references and constants when I see some real world code I get overwhelmed by the cataclysm of them.


There's a difference between syntax and semantics. Syntax is how you write it and semantics is what it means e.g. the const reference method parameters below.

alamahant wrote:
It is as if nobody hardly ever uses plain variables.


Plain variables are used in methods and functions. That's why they are also called temporary variables. They stay on the stack and once they are out of scope they are gone. But if you want something to survive the scope of a method, it should be dynamically allocated, which means it should be accessible through either a pointer or a reference. My C++ is very rusty but I think a reference is strongly typed pointer to an object, so whether you'll use a pointer or a reference depends on what you want to do with that memory and how you want to treat it.

alamahant wrote:
Most methods use const references for their parameters.


That's to instruct the compiler to throw an error at you if it sees code that changes the referenced objects in the method. Not that it can't be done, but then it becomes complicated and you should really want it.

alamahant wrote:
I feel extremely confused.
How can one get the grip of these things?
How did you do it?


It sinks in with time. You brain will eventually chew and swallow it. I started with C and needed to know the difference between a pointer and a local variable, because I couldn't implement even a sinple linked list without pointers.

Then when I started with C++ there were no references. Or at least there were no books that covered I think C++ standard from '99 which featured references. I think in 2002 I bougth Bjorne Stroustop's C++ book that covered it. But I didn't get the difference until I moved to Java where there are no pointers. Either way it was not that common to write in C++ with references back then.

Best Regards,
Georgi


Last edited by logrusx on Tue Dec 03, 2024 4:57 pm; edited 3 times in total
Back to top
View user's profile Send private message
pingtoo
Veteran
Veteran


Joined: 10 Sep 2021
Posts: 1305
Location: Richmond Hill, Canada

PostPosted: Tue Dec 03, 2024 4:47 pm    Post subject: Reply with quote

May I suggest give us examples so it will be easier to understand your point.

It will be better if you can give a cpp example the illustrate the point you made about current practice the confuse you and a second example that you believe will do that same as first example but use your idea of variables so we have a contrast that will help understand where your key confuse.
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1734
Location: South America

PostPosted: Tue Dec 03, 2024 4:55 pm    Post subject: Reply with quote

One important use of references is avoiding duplication of objects that might be expensive to duplicate. And doing a "move" instead of a copy when possible. Think objects of classes that have constructors and destructors that do a nontrivial amount of work. Simple objects of scalar type are not that expensive to copy, so code usually bothers less with references to those types.

When you "use plain variables", as you put it, there can be a lot of that duplication without you realizing it.

C code sometimes uses pointers to structure types for similar purposes, to avoid creating unnecessary copies of objects of those types.

And when pointers or references are used, specifying const makes the compiler yell at you if you inadvertently attempt to modify the object through the pointer or reference in code that is not supposed to, as logrusx said. And that is good for avoiding bugs.

Posting an example that uses references where you would be inclined to "use plain variables", as pingtoo suggests, might help us better explain this idea to you.
_________________
NeddySeagoon wrote:
I'm not a witch, I'm a retired electronics engineer :)
Ionen wrote:
As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though :)
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9615
Location: beyond the rim

PostPosted: Wed Dec 04, 2024 8:32 am    Post subject: Reply with quote

The main reason to use const references instead of regular variables is to avoid data transfers. For scalar variables it doesn't really matter, but for more complex data types it avoids unnecessary copying and potentially expensive construction of temporary variables. In practice it shouldn't really matter to you when calling a function/method wether the parameter is a const reference or a plain copy-by-value parameter as they both accept r-values. Mind that does not apply to non-const references, those are usually used for in-out parameters in C++ (while C usually uses pointers in that case) which restricts any inputs to l-values.

(if you don't know what r-value and l-value mean you really should look it up).
Back to top
View user's profile Send private message
SiberianSniper
Guru
Guru


Joined: 06 Apr 2006
Posts: 381
Location: Dayton, OH, USA

PostPosted: Wed Dec 04, 2024 5:43 pm    Post subject: Reply with quote

One other thing to mention is the difference between passing pointers vs. references. Once compiled, the machine code will usually be identical, so you may wonder when to use each one in favor of the other In general, using a pointer signifies that the object may or may not exist (so you need to check for nullptr), whereas a reference means that the object does exist (unless you're intentionally doing stupid language tricks).

Also, raw pointers are effectively deprecated for *most* purposes, so you may want to look into the C++11 smart pointers (unique_ptr, shared_ptr, and weak_ptr).
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum