Struct Constructor in C++?

前端 未结 16 1809
轻奢々
轻奢々 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条回答
  •  Happy的楠姐
    2020-11-27 09:17

    Yes. A structure is just like a class, but defaults to public:, in the class definition and when inheriting:

    struct Foo
    {
        int bar;
    
        Foo(void) :
        bar(0)
        {
        }
    }
    

    Considering your other question, I would suggest you read through some tutorials. They will answer your questions faster and more complete than we will.

提交回复
热议问题