Let’s say I have a struct that has some number of fields, and I’m probably going to be messing around with them (adding, removing).
struct Something
{
int number;
int stuff[4];
//... many other things
Something(const Something& something, int newnumber) : number(newnumber)
{
//is there any way to default the rest to copy?
}
};
I’m not declaring a copy constructor (using the default), so it will obviously handle any basic case (no pointers and the like). However, I also would like to be able to copy my struct with one thing changed. That would mean I need to fill out an initializer list with all of the things to copy, and change it everytime my struct contents change. Even though my undeclared copy constructor will conveniently handle changes with no intervention on my part.
Is there any way to take advantage of that default copy constructor so I don’t have to change my “copy with one change” constructor everytime I mess with my struct contents?
edit:
Constructor delegation results in these errors:
Something(const Something& something, int newnumber) : Something(something), number(newnumber) {}
a call to a delegating constructor shall be the only member-initializer
‘number’ : already initialized
You can’t do that because the default constructor already initializes
number, so you would be initializingnumbertwice. The best you can do is move number(newnumber) inside the body of the ctor, i.e.number = newnumber;. As a general rule, you should put the logic in the constructor with most arguments and delegate all other constructors to it (perhaps this isn’t possible in your example though).