0

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.

2
  • I think it depends on your software design.
    – Seyf
    Oct 23, 2014 at 11:58
  • You could consider an object-oriented design and store the value as a member variable in a class and then have functionA and functionB as member functions on your class. Then you don't need to pass the value through the functions.
    – Chris Drew
    Oct 23, 2014 at 12:11

3 Answers 3

3

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.

0

Think about an interface that encapsulates that calls functionB

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.