Struct Constructor in C++?

前端 未结 16 1698
轻奢々
轻奢々 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: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.

    0 讨论(0)
  • 2020-11-27 09:17

    Syntax is as same as of class in C++. If you aware of creating constructor in c++ then it is same in struct.

    struct Date
    {
        int day;
    
        Date(int d)
        {
            day = d;
        }
    
        void printDay()
        {
            cout << "day " << day << endl;
        }
    };
    

    Struct can have all things as class in c++. As earlier said difference is only that by default C++ member have private access but in struct it is public.But as per programming consideration Use the struct keyword for data-only structures. Use the class keyword for objects that have both data and functions.

    0 讨论(0)
  • 2020-11-27 09:19
    struct TestStruct {
            int id;
            TestStruct() : id(42)
            {
            }
    };
    
    0 讨论(0)
  • 2020-11-27 09:20

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

    #include<iostream.h> 
    struct a {
      int x;
      a(){x=100;}
    };
    
    int main() {
      struct a a1;
      getch();
    }
    
    0 讨论(0)
  • 2020-11-27 09:23

    Note that there is one interesting difference (at least with the MS C++ compiler):


    If you have a plain vanilla struct like this

    struct MyStruct {
       int id;
       double x;
       double y;
    } MYSTRUCT;
    

    then somewhere else you might initialize an array of such objects like this:

    MYSTRUCT _pointList[] = { 
       { 1, 1.0, 1.0 }, 
       { 2, 1.0, 2.0 }, 
       { 3, 2.0, 1.0 }
    };
    

    however, as soon as you add a user-defined constructor to MyStruct such as the ones discussed above, you'd get an error like this:

        'MyStruct' : Types with user defined constructors are not aggregate
         <file and line> : error C2552: '_pointList' : non-aggregates cannot 
         be initialized with initializer list.
    

    So that's at least one other difference between a struct and a class. This kind of initialization may not be good OO practice, but it appears all over the place in the legacy WinSDK c++ code that I support. Just so you know...

    0 讨论(0)
  • 2020-11-27 09:28

    All the above answers technically answer the asker's question, but just thought I'd point out a case where you might encounter problems.

    If you declare your struct like this:

    typedef struct{
    int x;
    foo(){};
    } foo;
    

    You will have problems trying to declare a constructor. This is of course because you haven't actually declared a struct named "foo", you've created an anonymous struct and assigned it the alias "foo". This also means you will not be able to use "foo" with a scoping operator in a cpp file:

    foo.h:

    typedef struct{
    int x;
    void myFunc(int y);
    } foo;
    

    foo.cpp:

    //<-- This will not work because the struct "foo" was never declared.
    void foo::myFunc(int y)
    {
      //do something...
    }
    

    To fix this, you must either do this:

    struct foo{
    int x;
    foo(){};
    };
    

    or this:

    typedef struct foo{
    int x;
    foo(){};
    } foo;
    

    Where the latter creates a struct called "foo" and gives it the alias "foo" so you don't have to use the struct keyword when referencing it.

    0 讨论(0)
提交回复
热议问题