简体   繁体   中英

How to override non-class members?

I'm having problems trying to override non-class members but I get the error: "'override' can only be specified on class members".

How can I get around this?

override func ^(lhs: Int, rhs: Int) -> Int {
    return Int(pow(Double(lhs), Double(rhs)))
}

I thought it would work OK, but I didn't expect an error to come up!

You cannot use override in only file-scoped place. It uses within class scope.

Don't use ^ sign to make new function as you want. Here below is description of original function ^ :

/// Returns the result of performing a bitwise XOR operation on the two given
/// values.
///
/// A bitwise XOR operation, also known as an exclusive OR operation, results
/// in a value that has each bit set to `1` where *one or the other but not
/// both* of its arguments had that bit set to `1`. For example:
///
///     let x: UInt8 = 5          // 0b00000101
///     let y: UInt8 = 14         // 0b00001110
///     let z = x ^ y             // 0b00001011
///     // z == 11
///
/// - Parameters:
///   - lhs: An integer value.
///   - rhs: Another integer value.
public static func ^ (lhs: Int, rhs: Int) -> Int

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