struct in class

前端 未结 6 1154
梦毁少年i
梦毁少年i 2020-12-30 05:09

I have struct in class and not know how to call variables from struct, please help ;)

#include 
using namespace std;

class E
{
public: 
            


        
6条回答
  •  感情败类
    2020-12-30 06:00

    It's not clear what you're actually trying to achieve, but here are two alternatives:

    class E
    {
    public:
        struct X
        {
            int v;
        };
    
        // 1. (a) Instantiate an 'X' within 'E':
        X x;
    };
    
    int main()
    {
        // 1. (b) Modify the 'x' within an 'E':
        E e;
        e.x.v = 9;
    
        // 2. Instantiate an 'X' outside 'E':
        E::X x;
        x.v = 10;
    }
    

提交回复
热议问题