简体   繁体   中英

What does this piece of C++ code do?

I am reading a WinSock tutorial, and I notice the following line of code:

bind(listener, (SOCKADDR*)(&clientinfo), sizeof(clientinfo))

listener is a SOCKET object, clientinfo is a SOCKADDR_IN object.

I know & is the "address-of" operator, and * is the deference operator. I notice it looks similar to casting in C#.

What does: (SOCKADDR*)(&clientinfo) mean in the context of that function?

(&clientinfo) takes address of clientinfo and (SOCKADDR*) typecasts this to pointer to SOCKADDR

In C++ , I would prefer to use static_cast<SOCKADDR*>(&clientinfo)

You can read more about static_cast here .

It's a cast. It casts a pointer to a SOCKADDR_IN into a pointer to a SOCKADDR. These two types happen to be compatible.

It's basically how polymorphism is done in C, and bind is a C API.

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