samedi 23 janvier 2021

Why the copy constructor was called(statick_cast)

class TokenB {
    public:
    TokenB() {cout << "default B ";}
};

class TokenD: public TokenB{
    public:
    TokenD(){cout << "default D ";}
    explicit TokenD(const TokenB &t){cout<< "conversion D";}
    TokenD(const TokenD &t){cout << "copy D ";}
};

int main() {
    TokenB d;
    TokenD b = static_cast<TokenD&>(d); //copy constructor
    //TokenD b = static_cast<TokenD>(d); //conversion cosntructor
    return  0;
}

my question is how static_cast in this code work and if static_cast create temp object for both constructors(copy and conversion)

and difference between

  • static_cast<TokenD&>
  • static_cast<TokenD>

Aucun commentaire:

Enregistrer un commentaire