Answered by:
How to create copy constructor in C++/CX with class having properties

Question
-
I'm new to C++/CX. I want to create a Vector class with two properties X and Y.
In standard C++, the copy constructor is:Vector(const Vector& v);
I translate that to C++/CX as:
Vector(const Vector^ v);
Here's the class:
Header:ref class Vector { public: Vector(); Vector(const Vector^ v); property double X; property double Y; };
Implementation:
Vector::Vector() { X = 0; Y = 0; } Vector::Vector(const Vector^ v) { this->X = v->X; this->Y = v->Y; }
But I got an error when assigning v->X to this->X as: no instance of function "Vector::X::get" matches the argument list and object (the object as type qualifiers that prevent a match).
How to implement the copy constructor correctly?
Thanks.- Moved by Helen Zhao Monday, July 23, 2012 9:39 AM (From:Visual C++ General)
Sunday, July 22, 2012 10:05 AM
Answers
-
Hi,
I agree with Giovanni. You should delete the const keyword. And I suggest you refer to the following codes for more information about creating a ref class with two properties:
// the header file ref class Factory { private: double _x; double _y; public: Factory(); Factory( Factory^ f); property double X { double get(){return _x; } void set(double x) { if(x!=_x) { _x=x; } } } property double Y { double get(){ return _y;} void set(double y) { if(_y!=y) { _y=y; } } } };
//the cpp file Factory::Factory() { _x=0; _y=0; } Factory::Factory( Factory^ f) { this->X=f->X; this->Y=f->Y; }
I hope this reply is helpful to you. If you have any questions, please feel free to let me know.
Best regards,
Helen Zhao [MSFT]
MSDN Community Support | Feedback to us
- Edited by Helen Zhao Monday, July 23, 2012 10:17 AM
- Proposed as answer by DavidLambMicrosoft employee, Moderator Wednesday, July 25, 2012 3:00 PM
- Marked as answer by Jesse Jiang Thursday, August 2, 2012 7:16 AM
Monday, July 23, 2012 10:17 AM
All replies
-
On 22/07/2012 12:05, Tyler Nguyen wrote:
I'm new to C++/CX. I want to create a Vector class with two properties X and Y.
In standard C++, the copy constructor is:Vector(const Vector& v);
I translate that to C++/CX as:
Vector(const Vector^ v);
[...]
But I got an error when assigning v->X to this->X as: no instance of function "Vector::X::get" matches the argument list and object (the object as type qualifiers that prevent a match).
I'm not sure that it makes sense to have a copy constructor in C++/CX, since the "ref" classes are managed by reference (and the "^" is kind of like a CComPtr smart pointer).
In any case, I'd try removing the 'const' (I'm not sure there's a strong support for const-correctness in C++/CX like there is in C++):
Vector(Vector^ v)
Giovanni
- Proposed as answer by DavidLambMicrosoft employee, Moderator Wednesday, July 25, 2012 3:00 PM
Sunday, July 22, 2012 3:22 PM -
Hi Tyler,
I'd like to move this thread to Building Metro style apps with C++ Forum for more efficient responses.
Thanks for your understanding.
Best regards,
Helen Zhao [MSFT]
MSDN Community Support | Feedback to us
Monday, July 23, 2012 9:39 AM -
Hi,
I agree with Giovanni. You should delete the const keyword. And I suggest you refer to the following codes for more information about creating a ref class with two properties:
// the header file ref class Factory { private: double _x; double _y; public: Factory(); Factory( Factory^ f); property double X { double get(){return _x; } void set(double x) { if(x!=_x) { _x=x; } } } property double Y { double get(){ return _y;} void set(double y) { if(_y!=y) { _y=y; } } } };
//the cpp file Factory::Factory() { _x=0; _y=0; } Factory::Factory( Factory^ f) { this->X=f->X; this->Y=f->Y; }
I hope this reply is helpful to you. If you have any questions, please feel free to let me know.
Best regards,
Helen Zhao [MSFT]
MSDN Community Support | Feedback to us
- Edited by Helen Zhao Monday, July 23, 2012 10:17 AM
- Proposed as answer by DavidLambMicrosoft employee, Moderator Wednesday, July 25, 2012 3:00 PM
- Marked as answer by Jesse Jiang Thursday, August 2, 2012 7:16 AM
Monday, July 23, 2012 10:17 AM