Struct Constructor in C++?

前端 未结 16 1813
轻奢々
轻奢々 2020-11-27 08:53

Can a struct have a constructor in C++?

I have been trying to solve this problem but I am not getting the syntax.

16条回答
  •  爱一瞬间的悲伤
    2020-11-27 09:20

    Yes it possible to have constructor in structure here is one example:

    #include 
    struct a {
      int x;
      a(){x=100;}
    };
    
    int main() {
      struct a a1;
      getch();
    }
    

提交回复
热议问题