代码之家  ›  专栏  ›  技术社区  ›  Jasarien

在C/Objective-C中,将返回值放在括号中的目的是什么?

  •  4
  • Jasarien  · 技术社区  · 15 年前

    那有什么用?

    我看到的代码生成了一个图像,调整了大小,然后返回。

    - (UIImage *)resizeImage:(UIImage *)image
    {
        //
        // some fascinating, but irrelevant, resizing code here
        //
    
        return (image);
    }
    
    4 回复  |  直到 15 年前
        1
  •  5
  •   John Bode    15 年前

    至少就C而言,这没什么区别。paren不是必需的,但它们不会改变return语句的含义。return语句的语法是

    return-statement:
        return expressionopt ;
    

    其中一个作品 表达 非终端是 带圆括号的表达式 ( 表达

        2
  •  4
  •   Tomas    15 年前

    它重写运算符优先级,但是没有任何运算符的优先级低于“return”。

        3
  •  1
  •   Sam Skuce    15 年前

    简而言之,这只是一种风格选择,比如使用“/*comments*/”而不是“//comments”

        4
  •  0
  •   Splashdust    15 年前

    在您的例子中,这相当于键入不带括号的返回。通常,您会使用括号进行类型转换,或者如果希望将表达式视为独立块。

    例如:

    // This is an untyped pointer to an instance of SomeClass
    void* ptr = instance;
    
    // In order to let the compiler know that ptr is an instance of SomeClass
    // we cast it, and then we put the cast in parentheses to be able to access
    // a property on the result of the cast.
    return ((SomeClass *)ptr).someproperty;