tags:

views:

4976

answers:

7

Can a struct have a constructor in C++?

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

+6  A: 
struct TestStruct {
        int id;
        TestStruct() : id(42)
        {
        }
};
nos
Nice Constant...
dicroce
+24  A: 

In C++ the only difference between a class and a struct is that class-members are private by default, while struct-members default to public. So structures can have constructors, and the syntax is the same as for classes.

sth
And that structures will default to public when deriving from :)
GMan
@sth Your right on the difference between struct and class, however I think he's having a compile issue. The issue might be because of a union that is using the struct. You can't have non-trivial constructors in the type you have in a union.
Chap
@Chap: If he has concrete problems where the general solution doesn't work, it would probably be the best idea to post some code that shows the problem and the compiler errors that are generated. But as general as the question is asked I don't think one can really infer too much about the concrete problem the OP is trying to solve...
sth
@sth: Your reputation has been recalculated, as you requested. You must be good at spotting which questions have a high likelihood of staying open, because you barely lost any. :)
Bill the Lizard
@Bill the Lizard: Yeah, the "loss" of this answer here was probably the biggest hit, and it was less than I expected, so I'm quite happy with it :)
sth
+2  A: 

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.

GMan
+4  A: 

Yes structures and classes in C++ are the same except that structures members are public by default whereas classes members are private by default. Anything you can do in a class you should be able to do in a structure.

struct Foo
{
  Foo()
  {
    // Initialize Foo
  }
};
heavyd
+9  A: 

Yes, but if you have your structure in a union then you cannot. It is the same as a class.

struct Example
{
   unsigned int mTest;
   Example()
   {
   }
};

Unions will not allow constructors in the structs. You can make a constructor on the union though. This question relates to non-trivial constructors in unions.

Chap
yep..thank u @chap
A: 

In C++, we can declare/define the structure just like class and have the constructors/destructors for the Structures and have variables/functions defined in it. The only difference is the default scope of the variables/functions defined. Other than the above difference, mostly you should be able to imitate the functionality of class using structs.

Roopesh Majeti
A: 
struct HaveSome
{
   int fun;
   HaveSome()
   {
      fun = 69;
   }
};

I'd rather initialize inside the constructor so I don't need to keep the order.

SwDevMan81