r/backtickbot • • Dec 16 '20

https://np.reddit.com/r/badcode/comments/ke6mae/there_cant_be_a_better_algorithm_for_counting_how/gg26i4h/

It depends on how the value is passed in! Say we have some function, aFunc:

// x is a copy of the original value passed in, and x can be mutated. It will not affect the original value.

void aFunc( int x );


// x is a pointer to the original value passed in, and x can be mutated. It will affect the original value. Access the x pointer’s value using the -> operator.

void aFunc( int * x );


// x is a reference to the original value passed in, and x can be mutated. It will affect the original value. Access the x pointer’s value using the . operator.

void aFunc( int & x );

Additionally you also have the const modifier you can apply to variables / parameters / functions.

// x is a copy of the original value passed in, and x cannot be mutated. (Kinda silly.) Changing it would not affect the original value.

void aFunc( int const x );

// x is a pointer to the original value passed in, and x cannot be mutated. Changing it would affect the original value. Access the x pointer’s value using the -> operator.

void aFunc( int const * x );


// x is a reference to the original value passed in, and x cannot be mutated. Changing it would affect the original value. Access the x reference’s value using the . operator.

void aFunc( int const & x );

There are more variations of the above, but these are the ones you’re most likely to encounter IMO

0 Upvotes

0 comments sorted by