Author |
Topic |
|
RoSe
Junior Member
15 Posts |
Posted - Mar 04 2008 : 03:05:23 AM
|
Sometimes I have data classes in which I define my fields and then need some constructors with different combinations of parameters that will be assigned to the fields. It would be convenient to have a refactor method on selected fields of a class like 'create constructor' that creates a constructor containing a parameter for each selected field and in the body of the constructor the parameters will be automatically assigned to the matching field. |
|
feline
Whole Tomato Software
United Kingdom
19022 Posts |
Posted - Mar 07 2008 : 2:48:54 PM
|
This is an interesting idea. I am in two minds about this, I can see the appeal, but I can also see some problems. My main concern is how do you select the fields you want the constructor to set?
If the class looks like:
class CTest
{
private:
int nOne, nTwo, nThree, nFour;
};
then how do you select members one and four?
A refactoring operation to create a constructor that initialises all member variables with parameters would avoid this selection problem, but you would then need to manually delete the parameters you did not want. |
zen is the art of being at one with the two'ness |
Edited by - feline on Mar 07 2008 2:50:22 PM |
|
|
RoSe
Junior Member
15 Posts |
Posted - Mar 10 2008 : 08:48:58 AM
|
Well, you are right. I would make the decision line by line: each line that is marked will be included with all its members. In your example that would include all four members. I think the availability of such a refactoring method will make me think of how to sort my members to ease the writing of constructors ... and that would avoid the situation you mentioned. |
|
|
feline
Whole Tomato Software
United Kingdom
19022 Posts |
Posted - Mar 10 2008 : 11:23:13 AM
|
VS2005 is not letting me select two separate lines. I can select a single line, or a block of lines, but not separate lines.
So this means that for the class:
class CTest
{
private:
int nOne;
int nTwo;
int nThree;
int nFour;
}; there is no way to select "nOne" and "nFour". You have to select just one of them, or all 4 members at once.
For a large class this would require forcing you to reorder the class members to group the members together that you need to pass to the constructor. |
zen is the art of being at one with the two'ness |
|
|
accord
Whole Tomato Software
United Kingdom
3287 Posts |
Posted - Mar 10 2008 : 5:50:10 PM
|
VA Outline? |
|
|
feline
Whole Tomato Software
United Kingdom
19022 Posts |
Posted - Mar 11 2008 : 09:33:56 AM
|
An interesting thought. A very interesting thought...
There is a possible conflict here. If I select three class members in VA Outline and then trigger Encapsulate Field, which is basically what we are doing here, what should happen?
Currently Encapsulate Field only operates on a single class member at a time, but there are regular requests to allow the refactoring operations to operate on several functions at once.
Is the constructor sufficiently different to normal Encapsulate Field to warrant its own special command? If you format it as:
CTest(int one, int four) : nOne(one), nFour(four)
{
} then it probably is.
I have put in a feature request for this to see what our developers make of it. I can see this being rather useful now and then:
case=14340 |
zen is the art of being at one with the two'ness |
|
|
dinaiz
Junior Member
France
13 Posts |
Posted - Feb 06 2012 : 9:11:00 PM
|
Hi ! I know this thread is quite old but I still think it's an important and missing feature !
IMHO, something really simple would do, like from :
class CTest
{
private:
int nOne;
int nTwo;
int nThree;
int nFour;
};
have the option to generate :
class CTest
{
private:
int nOne;
int nTwo;
int nThree;
int nFour;
public:
Ctest(int one, int two, int three, int four) : nOne(one),nTwo(two),nTwo(three), nFour(four)
{
}
};
You can easily declare only the fileds which need initilialization, generate the constructor, then add the rest of the fields. That's what I do manually most of the time anyway :)
Or do you think there would be a way to get away with this using code snippets ?
|
|
|
dinaiz
Junior Member
France
13 Posts |
|
feline
Whole Tomato Software
United Kingdom
19022 Posts |
Posted - Feb 07 2012 : 2:30:19 PM
|
An interesting link, thank you for this dinaiz. This is still a feature we are looking into adding, along with the related idea of just generating a default constructor for all members of a class.
Unfortunately I don't currently have an estimate for when this will be added to VA. |
zen is the art of being at one with the two'ness |
|
|
joostn
Junior Member
16 Posts |
Posted - Jan 21 2016 : 05:26:39 AM
|
I'd like to add my +1 to this request!
Just keep it simple: generate a constructor which includes all member variables in the listed order. I can remove or reorder fields afterwards. |
|
|
joostn
Junior Member
16 Posts |
Posted - Jan 21 2016 : 05:34:12 AM
|
And while I'm at it, this is what I could use as well:
class T
{
private:
int m_Member1;
float m_Member2;
};
I often need to add a member function Tuple() as follows:
class T
{
public:
auto Tuple() const
{
return std::tie(m_Member1, m_Member2);
}
private:
int m_Member1;
float m_Member2;
};
This Tuple() function can be used to generate all comparison operators in one go using a mixin class. And it would probably be useful for serialization as well.
So basically I'd just need a VAX function which generates a list of all member variables separated by commas. |
|
|
feline
Whole Tomato Software
United Kingdom
19022 Posts |
Posted - Jan 21 2016 : 4:26:43 PM
|
An interesting thought, I have put in a feature request for a VA Snippet reserved string that would generate the list of class member variables, to see what our developers make of it:
case=94606 |
zen is the art of being at one with the two'ness |
|
|
|
Topic |
|