+ 2
Please help me to understand the Foo(const Foo&) statement in the example and when it will be executed.
This is from a c++ challenge and I don't understand the Foo(const Foo&) statement. I understand the the result is aaaaa, but how can I ever get a b? struct Foo { Foo() { cout << "a"; } Foo(const Foo&) { cout << "b"; } }; int main () { vector<Foo> bar(5); return 0; }
7 Answers
+ 5
To answer your question, when does the copy constructor gets invoked? the Foo(const Foo&) is executed when you create a Foo instance while passing other Foo instance as an argument for the copy constructor. For example:
int main () {
vector<Foo> bar(5);
Foo beer(bar[0]);
return 0;
}
But seriously, I can't help wondering, how the code runs just fine, while the copy constructor's argument appears incomplete (?), the constructor accepts an argument of constant Foo reference, but there is no parameter's name? and yet no compile error message for a missing argument name.
(Edit)
I have reposted the question in SoloLearn Fans Discord server, hopefully someone sees it and would say something to clear the doubts.
+ 4
@Ipang The parameter name is not required. But that also means that said parameter cannot be used in the function/constructor.
This allows for the destinction between post- and preincrement operators for example.
T& T::operator++(); // Pre-increment overload
T T::operator++(int); // Post-increment overload
The parameter itself is not used inside the function.
Parameter names can also be skipped in function prototypes, yet be present in the actual definition and still be used inside the function.
+ 3
Dennis Thank you very much for the answer, I learned something new again today : )
If you don't mind mate, I'm still not understanding the example of the pre & post increment, the anonym int argument is there only to distinct pre & post? it's not used at all is it? we increment private member's data when we do that right?
I think I need help understanding when and where anonym parameter is used, apart from function prototype : )
+ 3
Bravo Dennis mate! Thanks again for that, it's nice to have such a helpful community like this : )
mimtelf apologies for a little discussion, I have no intention to trash the thread, I just had a little doubt, and now it's clear (thanks for Dennis' points) I hope what we discussed here can come useful for other learners as well : )
Cheers all!
+ 2
@Ipang The int parameter is not used at all. It's a dummy parameter, as they say, its only purpose is to differentiate between the post and pre increment/decrement operator.
Its value is always 0, unless the user calls it like this a.operator++(2); But no one does that so ignore that one, still nice to know though.
Since the pre- and postincrement/decrement operator always increment/decrement by 1, there is no need for a value to be passed to it and you can simply hardcode it in.
+ 2
Sketch you're welcome to join! you can find the invite in the following thread
https://www.sololearn.com/Discuss/689391/?ref=app
+ 1
Ipang SoloLearn Fans Discord Server? wow let me join