[try Beta version]
Not logged in

 
A sudden question just come up with

Aug 24, 2015 at 12:51pm
How are they different in C++? [] and {}

And how do you call them?

I just wonder why we write as str {"hello"}, not str["hello"] or just str {hello}, str[hello].

thanks.
Aug 24, 2015 at 1:02pm
None of your post makes any sense. There is never a case where you could interchange square brackets with curly braces - they are very different. Curly braces are always used for scopes and uniform initialization.

Could you post some example code of what you are talking about?
Last edited on Aug 24, 2015 at 1:17pm
Aug 24, 2015 at 1:15pm
{} can be used with many things in C++

Variable declarations:
 
int variableInt {5};

Class declarations:
1
2
3
4
5
6
7
class Example
{
public:
    //
private:
    //
};

[] can be used for accessing a certain element of an array or vector
1
2
3
4
5
//declare array
int numbers[5] = {5, 6, 7, 8, 9};

//display first element
cout << numbers[0] << endl;
Last edited on Aug 24, 2015 at 1:32pm
Aug 24, 2015 at 2:08pm
To LB / I was about to ask you to determine when or which situation I am supposed to use {} or []. and now " There is never a case where you could interchange square brackets with curly braces" makes me think they are very obviously different in C++ and there's no rule to determine which to use over all the C++ , but just have to memorize it.
Thanks.

To Lbkulinski / Thank you so I have to memorize which braces should be used for each situation, and there are no rules to determine it over all the C++?
Aug 24, 2015 at 3:01pm
Knowing what braces to use when is something you learn quickly - they have very specific uses. I don't think 'memorize' is the right word though. Just read and write lots of code and you will get used to it.
Aug 25, 2015 at 3:08am
Yeah Thanks a lot !
Aug 25, 2015 at 4:29am
There are "rules", very strict rules. The C++ Standard is the rules. Simply reading the Tutorials on this site very carefully should give a good start.
Topic archived. No new replies allowed.