Parameters in function in separate initialization and definition | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Parameters in function in separate initialization and definition

When a function's initialization and definition is split, and it requires parameters on them both, how do I format that? In this code (excluding include and namespace): class CVector { public: int x,y; CVector () {}; CVector (int a,int b) : x(a), y(b) {} CVector operator + (const CVector&); }; CVector CVector::operator+ (const CVector& param) { CVector temp; temp.x = x + param.x; temp.y = y + param.y; return temp; } int main () { CVector foo (3,1); CVector bar (1,2); CVector result; result = foo + bar; cout << result.x << ',' << result.y << '\n'; return 0; } The CVector constructor initialization parameter is an anonymous CVector reference, but in the definition, it is a CVector reference called param. So, when having them split, should I have an anonymous data type in the init and a reference variable in the definition? Thanks!

16th Oct 2020, 3:23 AM
elide
elide - avatar
1 Answer
+ 1
It is ok to only define the type of parameter in declaration (name is not mandatory), name is mandatory in the definition, because a need to refer to the parameter is likely to happen.
16th Oct 2020, 3:56 AM
Ipang