C ++类 - 派生类中的构造函数声明

时间:2021-03-24 14:01:53

Socket has a constructor that takes a winsock SOCKET as parameter and stores it in a private variable:

Socket有一个构造函数,它将winsock SOCKET作为参数并将其存储在一个私有变量中:

Socket::Socket(SOCKET s) {
    this->s = s;
}

I'm trying to make a class "GameSocket" that will parse data from my Socket class:

我正在尝试创建一个类“GameSocket”来解析我的Socket类中的数据:

class GameSocket : public Socket {

protected:

    void ParseData(unsigned char* data, int size);

};

Next to these classes, I have a "Server" class that creates new sockets when needed:

在这些类的旁边,我有一个“Server”类,可以在需要时创建新的套接字:

GameSocket* Server::Accept() {

    SOCKET a = accept(s, 0, 0);
    if(a==SOCKET_ERROR) {
        return 0;
    }
    else {
        return new GameSocket(a);
    }

}

However, this gives me an error on the last "else":

但是,这给了我最后一个“其他”的错误:

error C2664: 'GameSocket::GameSocket' : cannot convert parameter 1 from 'SOCKET' to 'const GameSocket &'

I must be missing something with constructors when dealing with derived classes...

在处理派生类时,我必须遗漏构造函数...

Don't go too hard on me, I'm relatively new to C++ and OOP

不要对我太过刻意,我对C ++和OOP比较陌生

2 个解决方案

#1


Add in a constructor for GameSocket

添加GameSocket的构造函数

class GameSocket : public Socket {

public:

    // you need to add
    GameSocket(SOCKET s) : Socket(s) {}

protected:

    void ParseData(unsigned char* data, int size);

};

#2


The construcotr for GameSocket must receive a SOCKET parameter and then pass it to the Socket base class in the initializer list:

GameSocket的construcotr必须接收一个SOCKET参数,然后将它传递给初始化列表中的Socket基类:

class GameSocket : public Socket {
public:
    GameSocket(SOCKET s) : Socket(s) {}
    ...
};

Is there a reason why GameSocket must derive from Socket instead of holding a reference to the Socket? GameSocket is (or should be) managing socket state and serialization while the low level socket interface is contained in the Socket class. Your Server class could create instances of the Socket class and then pass a pointer to a GameSocket class for managing them.

是否有理由为什么GameSocket必须从Socket派生而不是持有对Socket的引用? GameSocket是(或应该)管理套接字状态和序列化,而低级套接字接口包含在Socket类中。您的Server类可以创建Socket类的实例,然后将指针传递给GameSocket类来管理它们。

class GameSocket {
public:
    GameSocket(Socket *s) : s_(s) {}
    ~GameSocket() {
        s_->close();
        delete s_;
    }
    ...
private:
    Socket *s_;
};

GameSocket* Server::Accept() {
    // accept function added to Socket class
    Socket *newSocket = serverSocket->accept();
    // create GameSocket with newly opened Socket
    return newSocket ? new GameSocket(newSocket) : NULL;
}

#1


Add in a constructor for GameSocket

添加GameSocket的构造函数

class GameSocket : public Socket {

public:

    // you need to add
    GameSocket(SOCKET s) : Socket(s) {}

protected:

    void ParseData(unsigned char* data, int size);

};

#2


The construcotr for GameSocket must receive a SOCKET parameter and then pass it to the Socket base class in the initializer list:

GameSocket的construcotr必须接收一个SOCKET参数,然后将它传递给初始化列表中的Socket基类:

class GameSocket : public Socket {
public:
    GameSocket(SOCKET s) : Socket(s) {}
    ...
};

Is there a reason why GameSocket must derive from Socket instead of holding a reference to the Socket? GameSocket is (or should be) managing socket state and serialization while the low level socket interface is contained in the Socket class. Your Server class could create instances of the Socket class and then pass a pointer to a GameSocket class for managing them.

是否有理由为什么GameSocket必须从Socket派生而不是持有对Socket的引用? GameSocket是(或应该)管理套接字状态和序列化,而低级套接字接口包含在Socket类中。您的Server类可以创建Socket类的实例,然后将指针传递给GameSocket类来管理它们。

class GameSocket {
public:
    GameSocket(Socket *s) : s_(s) {}
    ~GameSocket() {
        s_->close();
        delete s_;
    }
    ...
private:
    Socket *s_;
};

GameSocket* Server::Accept() {
    // accept function added to Socket class
    Socket *newSocket = serverSocket->accept();
    // create GameSocket with newly opened Socket
    return newSocket ? new GameSocket(newSocket) : NULL;
}