gpt4 book ai didi

c++ - 'operator sockaddr *()' 在这里是什么意思?

转载 作者:行者123 更新时间:2023-12-04 03:25:20 24 4
gpt4 key购买 nike

'operator sockaddr *()'在这里是什么意思?类 Raw 是一个内部类,sockaddr 是一个结构。


struct sockaddr
{
__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
char sa_data[14]; /* Address data. */
};

class Address {
public:
//! \brief Wrapper around [sockaddr_storage](@ref man7::socket).
//! \details A `sockaddr_storage` is enough space to store any socket address (IPv4 or IPv6).
class Raw {
public:
sockaddr_storage storage{}; //!< The wrapped struct itself.
operator sockaddr *(); // here
operator const sockaddr *() const;
};

private:
socklen_t _size; //!< Size of the wrapped address.
Raw _address{}; //!< A wrapped [sockaddr_storage](@ref man7::socket) containing the address.
.....
.....

最佳答案

这是一个 user defined conversion function . Raw 类型的对象可以隐式转换为 sockaddr 指针。例如以下将编译:

void fun(sockaddr *p); // function that takes a sockaddr pointer

Address::Raw r;
fun(r); // implicit conversion occurs from Raw in order to use function `fun`

关于c++ - 'operator sockaddr *()' 在这里是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67699276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com