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: 1304
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: 1730
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 to do 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 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 modify the object through the pointer or reference in code that is not supposed to. And that is good for avoiding bugs.

Posting an example that uses references where you would be inclined to "use 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
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