RAD Studio |  |

E2157: Deleting an object requires exactly one conversion to pointer operator (C++)

If a person uses the 'delete' operator on an object (note: not a pointer to an object, but an object itself), the standard requires that object to define exactly one "conversion to pointer operator" which will yield the pointer that gets freed. For example:

Copy Code

    char *a = new char[10];
    class foo {
    public:
        operator char *() { return a; }
    };

    int main() {
        delete[] x;
    }

Since 'x' is not a pointer, but an object, the compiler will delete 'a', because that is what the pointer conversion operator for the object yields. Having more than one conversion to pointer operator is illegal, because the compiler would not know which one to call.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!