kernelGinj
kernelGinj

Reputation: 103

Passing values or References through functions

This is a question of style or correctness for C++.

Say you have functionA and functionB, is it correct or good style to pass a value to functionA which it doesn't need itself, but needs to know about it to call functionB, which it(functionA) calls later?

I can't refer directly to the values as they are declared inside main.

Upvotes: 0

Views: 52

Answers (3)

user3477416
user3477416

Reputation:

A common convention for large variables in C++ is the use of pointers. To save resources, you can initialize a pointer to a variable on the heap using new like so: int *num = new int; This way you're not passing around a big array - for example - and instead, pass around the hex address of that array in memory.

Upvotes: 0

sfrehse
sfrehse

Reputation: 1072

Think about an interface that encapsulates that calls functionB

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234865

If functionB needs the value then, effectively, functionA does need it. Therefore, it should be passed as a parameter to functionA.

If you're worried about creating value copies, you could always pass it by reference or constant reference.

Upvotes: 3

Related Questions