简体   繁体   中英

C++ overloading typecast operator for pointers

I have a conversion like this:

Class1 *p1;
Class2 *p2 = new Class2();
p1 = (Class1 *) p2;

Can I override the typecast operator above to return a custom Class1 object pointer? If yes how?

EDIT: My exact problem is that I have code like this:

if (*$1 == ArrayType(AnyType()))
{
    $$ = ((ArrayType *) $1)->getElementsType();
}

Operator == is overloaded so $1 may be of type AnyType *.

No, you cannot overload conversion operators of non-class types.

What is the actual problem you want to solve? You might want to consider providing conversion operators in the actual classes.

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