简体   繁体   中英

C++ 'bracket' operators

In c++ when you use if you use these kinds of curly brackets:

if (x == y)
{ <---
} <---

Is it possible to create a class which uses these? Somewhere similar to this:

class x
{
    operator {} foo(int something)
    {
        do something...
    }
};

PS. I have no idea what these brackets are called, curly brackets?

Thank You

不可以,因为大括号不是运算符,并且您不能创建新的运算符-您只能重载现有的运算符。

Curly braces are actually used for many different things:

  • Compound statements
  • Initializers
  • Struct/enum/class/union/namespace specifier

(there's probably a few more too!)

None of these are operators and none of these can be overloaded.

What you may be interested in is C++11's brace initialization syntax which can you could use if you want a certain syntax:

class x
{
    x(int something)
    {
        do something...
    }
};

x foo{5};

Yup, they're called curly brackets or curly braces or curlies. They denote a block or a group of initializer values. You can't redefine their meaning.

(They are called curly brackets or curly braces, as far as I'm concerned.)

And they're not operators.

In the example with the if () you provided, they define the bounds of a scope. Or when it comes to OO, they define a namespace. Or a class.

And I believe you can't overload them (that would make no sense, really.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM