Union in c++ are they feasible

前端 未结 5 1799
死守一世寂寞
死守一世寂寞 2021-02-07 01:45

Can a union in C++ have a member function? How do union with data members and member functions exist if an object is created?

If I suppose yes, then are they feasible an

5条回答
  •  灰色年华
    2021-02-07 02:04

    I don't know if it's valid. Codepad accepts, runs, and gives the expected output from this program

    union x {
      int t;
      int k() { return 42;};
    };
    
    int main() {
      x y;
      y.t = y.k();
      std::cout << y.t << std::endl;
    }
    

提交回复
热议问题