#how to use references (should i be using pointers?)
85 messages · Page 1 of 1 (latest)
@safe cradle has reached level 1. GG!
because you have a reference in your struct
@raw thicket has reached level 2. GG!
in this case the compiler won't generate assignment operator for you
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.
@ancient python has reached level 31. GG!
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)
that doesn't make sense at all
@ancient python lol why are you nerd reacting?
because your message felt rudimentary, since you didnt point out what was wrong. probably I just oversimplified stuff, but still
wrong word
well references reference objects, just like pointers
*excessive
l-value references cannot reference "temporary objects" which would be rvalues
neither can pointers
but u dont store them just in variable
that what I tried to say
pointers specifically can not point to an object, they can be null
while references are always initialized
not sure what you mean by that
like I thought it is illegal in C++ to have class field like
Something& something;
that's perfectly legal
because it is uninitialized
well it won't compile if you don't initialize it, but you can initialize it
that doesn't mean it's not legal
and that has nothing to do with references pointing to "temporary" objects
and one of the defining characteristics of pointers is that they don't always point to an object
yeah
so thats why u use them as function argument instead of pointer to prevent ecxessive check on nullptr
well it depends
if the argument might be null/missing then it's fine to use a pointer
what can be example of usage
basically raw pointers should only be used as a nullable non-owning reference
if you want to store a reference to something
but why not just use pointer
because pointers can be null
but you can make setter that checks that
you don't always use getter/setter
so basically you just do it for
struct A
{
B& value;
};
A a;
B b;
a.value = b;
there's basically no difference between struct and class in C++
except the default access specifier
no, that wouldn't compile
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)
hmm?
like
A func1(B& b)
{
return blablabla(b);
}
A func2(B& b)
{
return func1(b);
}
...
B& b;
A a = func2(b);
you can use them as function arguments and you can use them in other places
just like other types
like func1 wont get B& b
because it needs a B b paremeter
no?
why not?
you're thinking of r-value references, &&
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
Yes
So I was thinking that without perfect forwarding you are unable to pass & further
so basically it has nothing to do with what we discussed so far
in this case it's completely unrelated
maybe check the relevant learncpp lessons again
Maybe
except that const lvalue references can
better to just overload the function...
or else your code ends up looking like python functions which have 20 parameters each 🤢
kinda, but with the lifetime extension it's not really a temporary anymore
that doesn't happen when you pass in a temporary to a function, right?