C++ define class member struct and return it in a member function

前端 未结 4 1022
南笙
南笙 2020-12-28 22:35

My goal is a class like:

class UserInformation
{
public:
    userInfo getInfo(int userId);
private:
    struct userInfo
    {
        int repu, quesCount, an         


        
4条回答
  •  不知归路
    2020-12-28 23:17

    It makes sense to make the nested structure type public, since the user code should be able to use it. Also, place the declaration of the structure before the point of its first use. Outside the class scope use scope resolution :: to refer to nested types.

    class UserInformation
    {
    public:
        struct UserInfo
        {
            int repu, quesCount, ansCount;
        };
    
    
    public:
        UserInfo getInfo(int userId);
    
    private:
        UserInfo infoStruct;
        int date;
    };
    
    UserInformation::UserInfo UserInformation::getInfo(int userId)
    {
        infoStruct.repu = 1000;
        return infoStruct;
    }
    

提交回复
热议问题