#how to use references (should i be using pointers?)

85 messages · Page 1 of 1 (latest)

safe cradle
#

guys im new to cpp and my vscode is yelling at me please help

#

sorry for profanity

#

it really doesnt like this move i tried to pull off lol

timid apexBOT
#

@safe cradle has reached level 1. GG!

raw thicket
#

because you have a reference in your struct

timid apexBOT
#

@raw thicket has reached level 2. GG!

raw thicket
#

in this case the compiler won't generate assignment operator for you

willow relic
#

It’s because of the reference type, from cpp reference:

Deleted copy assignment operator
An implicitly-declared or explicitly-defaulted(since C++11) copy assignment operator for class T is defined as deleted(since C++11) if any of the following conditions is satisfied:
….
T has a non-static data member of a reference type.

timid apexBOT
#

@ancient python has reached level 31. GG!

ancient python
#

Nice variable name, I cant even type it out

#

replace fellow& my_son with fellow* my_son

#

& is reference(pointing to temporary object), while * is pointer(pointing always to an object)

velvet steppe
velvet steppe
#

@ancient python lol why are you nerd reacting?

ancient python
#

wrong word

velvet steppe
#

well references reference objects, just like pointers

ancient python
#

*excessive

velvet steppe
#

l-value references cannot reference "temporary objects" which would be rvalues

#

neither can pointers

ancient python
#

that what I tried to say

velvet steppe
#

pointers specifically can not point to an object, they can be null

#

while references are always initialized

velvet steppe
ancient python
#

like I thought it is illegal in C++ to have class field like
Something& something;

velvet steppe
#

that's perfectly legal

ancient python
#

because it is uninitialized

velvet steppe
#

well it won't compile if you don't initialize it, but you can initialize it

ancient python
#

lol ok

#

never saw this in other code tho

velvet steppe
#

that doesn't mean it's not legal

#

and that has nothing to do with references pointing to "temporary" objects

ancient python
#

Ok my bad

#

I probably missed it with rvalue

velvet steppe
#

and one of the defining characteristics of pointers is that they don't always point to an object

ancient python
#

yeah

#

so thats why u use them as function argument instead of pointer to prevent ecxessive check on nullptr

velvet steppe
#

well it depends

#

if the argument might be null/missing then it's fine to use a pointer

ancient python
velvet steppe
#

basically raw pointers should only be used as a nullable non-owning reference

velvet steppe
ancient python
velvet steppe
#

because pointers can be null

ancient python
#

but you can make setter that checks that

velvet steppe
#

you don't always use getter/setter

ancient python
#

so basically you just do it for

struct A
{
  B& value;
};

A a;
B b;
a.value = b; 
velvet steppe
#

there's basically no difference between struct and class in C++

#

except the default access specifier

ancient python
#

well if I do it through constructor

#

Because I thought that & can be used only within function

#

And it wont work futher (like you should use perfect forwarding)

ancient python
#

like

A func1(B& b)
{
  return blablabla(b); 
}

A func2(B& b)
{
   return func1(b);
}
...
B& b;
A a = func2(b);
velvet steppe
#

you can use them as function arguments and you can use them in other places

#

just like other types

ancient python
#

because it needs a B b paremeter

#

no?

velvet steppe
#

why not?

ancient python
#

Maybe I remember it wrong

#

But when I tried that it didnt work

velvet steppe
#

you're thinking of r-value references, &&

ancient python
#

Maybe

#

That has to do something with perfect forwarding

velvet steppe
#

perfect forwarding is when you take a templated argument and you don't know if it's an l-value or r-value and you want to forward it with the same type to another function

ancient python
#

Yes

#

So I was thinking that without perfect forwarding you are unable to pass & further

velvet steppe
#

so basically it has nothing to do with what we discussed so far

ancient python
#

Okay

#

Thanks for explaining

velvet steppe
velvet steppe
ancient python
#

Maybe

eternal peak
eternal peak
#

or else your code ends up looking like python functions which have 20 parameters each 🤢

velvet steppe
eternal peak
#

that doesn't happen when you pass in a temporary to a function, right?

velvet steppe
#

yeah true

#

actually technically it does I think