C ++ Create Standard Constructor

I often find that I write very simple classes instead of C-style structures. They usually look like this:

class A { public: type mA; type mB; ... A(type mA,type mB,...) : mA(mA),mB(mB),... {} } 

Is this a smart way to do something? If so, I was wondering if there is any third-party plug-in or some convenient short text for automatically creating text for the constructor (for example, take selected or existing member definitions, replace commas with commas, move everything to the same line , ...)? Thanks

+6
source share
3 answers

Yes, just use simple aggregates:

 struct A { type mA; type mB; ... }; 

Using:

 A x = { mA, mB, ... }; 

The unit does not have customizable constructors, a destructor, and an assignment operator, and it allows for many optimizations. Aggregate initialization with binding syntax, for example, usually creates elements in place, even without a copy. You also get the best copy and move capabilities of the constructors and assignment operators defined by the compiler for you.

+11
source

EDIT: The previous version was c99, not C ++. now it is C ++

I think you can use {} initialization instead of writing a constructor. or is it in c ++ 0x? c99? I'm not sure. but it looks like this:

 struct A myA = { 3, 5}; 
+1
source

I would say that this is a reasonable way to do this, although OO fanatics may comment on your lack of encapsulation. I know that Visual Studio has built-in code snippets that do half the work you are looking for, but it depends on your IDE

0
source

Source: https://habr.com/ru/post/893598/


All Articles