POV-Ray : Newsgroups : povray.off-topic : Undirected ramblings on C++ : Re: Undirected ramblings on C++ Server Time
28 Jul 2024 12:26:12 EDT (-0400)
  Re: Undirected ramblings on C++  
From: Le Forgeron
Date: 27 Feb 2014 15:08:59
Message: <530f9b5b@news.povray.org>
Le 27/02/2014 20:23, Orchid Win7 v1 nous fit lire :
> Speaking of which... I think I may be fundamentally misunderstanding
> what references actually *do*. I had thought they were like pointers,
> just that you can't make them null. But some sources claim they're like
> pointers who's value can never change. So what, pray tell, does the
> following do?
> 
>   std::vectpr<int> foo;
>   std::vector<int> bar;
>   std::vector<int> & baz = foo;
>   baz = bar;
> 
> I had imagined that the final line changes baz from pointing to foo and
> to instead point to bar. But... if a reference can never change... then
> what the hell am I changing?? O_O

Reference are pointer which never changes (in the lifetime of the
reference), and which cannot be invalid (nullptr). But the referee can
be changed. (the object stay the same, but the content can change).

A non assigned reference cannot exist. The creation must assign it. And
once assigned, you cannot assign it a new value.
But that does not apply to the referenced content/object. it can be
change (or rather, updated).

foo is a sequence of int.
bar is a sequence of int.
baz is a reference to foo, so it's a sequence of int too.
foo, via baz, got its values replaced by the one of bar.

Gcc default to C++98 or C++03.. if you want more on the line of auto,
you need -std=c++0x or -std=c++11 (and you should use g++, as gcc is the
C front-end of the compiler, and auto has other meaning in C than in C++)

if you want real fun, look at lambda expression. The pointer to function
of C are just a dull feature... especially with some capture and some copy.

From C++ standpoint, you shouldn't need macro (excepted for protecting
include file with #ifndef FOO/define FOO/.../#endif... you have template
and other items like enum)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.