A
A
alexey_abramov2015-10-12 09:18:49
C++ / C#
alexey_abramov, 2015-10-12 09:18:49

How to implement a separate scope in 1 sishnik?

Hello. There is 1 si file. It has 1 variable and 3 functions. The variable must work with 2 functions, and the remaining function must not be able to work with it. Is it possible to implement this in one file? Or not? I tried to do it with curly braces, but it's impossible outside the function.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2015-10-12
@GavriKos

Perhaps of course. For example, do not make the variable global, but declare it in main and pass pointers to two other functions.

A
Alexander Latyshev, 2015-10-12
@magalex

It is not entirely clear what a variable means should work with functions. Perhaps you meant that the variable should be accessible from functions. Then the solution in the forehead:

функция1()
переменная
функция2()
функция3()

thus, function2 and function3 will be able to operate on the variable, but function1 will not.
But it's better to do as GavriKos suggested :
функция1()
функция2(переменная*)
функция3(переменная*)

thus, nothing depends on the order of function declaration, and you get rid of possible side effects in your program

X
Xano, 2015-10-12
@Xano

scope in the general case is calculated very simply:
in line N you can see everything that is written in lines from 0 to N - 1 (taking into account nesting levels of curly brackets)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question