+ 6
How do i know where to put brackets { } in c++ program?
i m very confused where should i put those curly brackets😢😢😢
3 Respostas
+ 8
Also in variable initialisation. (c++11)
int a {5};
char b {'b'};
etc
see: http://www.informit.com/articles/article.aspx?p=1852519
and
https://stackoverflow.com/a/18222927
+ 2
The opening bracket is for the beginning of a function, while the closing one is for the end.
For example:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!";
return 0;
}
The main function's content begins and ends with curly brackets.
Hope this helps :)
+ 2
also it help you to hide any variables to the local namespace.
#include <iostream>
int main()
{
int a = 1;
{
int b = 2;
cout << a << b; // OK. print 12
}
cout << a; // OK. print 1
cout << b; // error. undefined variable
}
This trick useful when some functional placed to destructor.