Author |
Topic |
|
str
Junior Member
19 Posts |
Posted - Oct 07 2020 : 09:38:18 AM
|
Hi, it would be nice, if it is possible to move entries from class initializer list to declaration.
class C { int a; // int a = 0; result };
C::C() : a(0) {}
Moving this a = 0 to declaration would be a great job for VAssist. Moving it manually for many entries is very error prone.
Best regards Steffen |
|
feline
Whole Tomato Software
United Kingdom
19023 Posts |
Posted - Oct 07 2020 : 10:52:09 AM
|
I am not sure what you are asking for. You cannot specify the value of a class member variable inside the class declaration.
You can initialize the member variable inside the constructor body, if that is what you are talking about.
Or are you trying to get VA to create the declaration for a new class member variable, based on it being initialized at the constructor? |
zen is the art of being at one with the two'ness |
|
|
str
Junior Member
19 Posts |
Posted - Oct 07 2020 : 11:33:10 AM
|
No, I mean exactly this. Since C++11 you are allowed t initialiize values in the class declaration (https://en.cppreference.com/w/cpp/language/data_members) Original:
class C
{
C::C() : a(0) {}
int a;
};
After refactor:
class C
{
C() = default;
int a = 0;
}; or
class C
{
C();
int a = 0;
};
C::C() = default; |
Edited by - str on Oct 07 2020 11:33:43 AM |
|
|
feline
Whole Tomato Software
United Kingdom
19023 Posts |
Posted - Oct 07 2020 : 12:12:59 PM
|
I had no idea this was valid C++ syntax, thank you for explaining. This is a lot clearer and makes the code more readable, making it a sensible refactoring to add. I have put in a feature request for this:
case=142935
Always something new to learn |
zen is the art of being at one with the two'ness |
|
|
Wolfram_Kuss
Starting Member
Germany
1 Posts |
Posted - Oct 25 2020 : 09:00:16 AM
|
That would be a very nice feature.
Actually, there are three ways to initialize a member during creation of the object: 1. An assignment "member = value;" in the constructor 2. The (member) initializer lists thre orignal poster speaks about 3. The initialization inside the class declaration.
I think it is general consensus (and I agree with it), that 3 is better than 2 and 2 is better than 1. So it would be nice, if the constructor begins with one or several "member = value;" assignments to also refactor these to use the third option.
That and doing code inspection on all files in a project would be my top wishes.
|
|
|
feline
Whole Tomato Software
United Kingdom
19023 Posts |
Posted - Oct 26 2020 : 07:09:41 AM
|
Moving the initializer from the constructor body to the class declaration makes sense, I have added this to the feature request.
As for Code Inspection, we are looking into running this across all of the files in the solution:
case=104537
we do want to make sure that we have a solid and reliable code inspection feature first though, so hopefully get it out of beta soon |
zen is the art of being at one with the two'ness |
|
|
|
Topic |
|