Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic constructor generation in C++?

imagine I have

struct Foo
{
   int a;
   string s;
   float f;
}

So now when I need to create new Foo I need to add a constructor:

struct Foo
    {
       int a;
       string s;
       float f;
       Foo(int a, string s, float f)
       {
          this->a = a;
          this->s = s;
          this->f = f;
       }
    }

However this method of manually writing constructors is really time consuming, especially with structs/classes with 10+ properties. My questions is: Is there a way to automatically generate such constructors?

like image 289
DELTA12 Avatar asked May 19 '26 03:05

DELTA12


1 Answers

struct Foo
{
  int a;
  std::string s;
  float f;
};

Foo f{42,"Foo",0.0};

works just fine, but a constructor gives you more control e.g. check of init value.

like image 197
AndersK Avatar answered May 20 '26 17:05

AndersK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!