More basic C++ questions - may I ask?
-
I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }
-
I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }
Using const that way in the declaration of the method tells the compiler that the method is able to be called on constant instances of that object. It indicates that the object is not altered in any way by calling that method. Examples using the class defined above:
const Point2d p1(2,3);
Point2d p2(4,5);p1.setCoords(3,2); // Compiler error. setCoords not declared as const method
p2.setCoords(5,4); // OKdouble n = p1.norm(); // OK - norm declared as const method
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
-
I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }
-
It's all in the documentation: https://msdn.microsoft.com/en-us/library/07x6b05d.aspx[^].
-
It was working this morning, because I read the content to see that it answered the question. Most likely a problem at Microsoft's end; keep trying. Or just use Google to find an alternate page.CodeProject bug, link now fixed.
There's a colon missing between the "https" and "//" in your link - but it's there in the link text. Maybe this a CodeProject bug?
-
There's a colon missing between the "https" and "//" in your link - but it's there in the link text. Maybe this a CodeProject bug?
-
I am still not too comfortable with passing variables to constructor, but eventually I'll get over it. But this got me stumped - never heard of having const used this way. Why? So how do you read this -X(x) with relation to following function double x()? Please if this is too basic for you just let someone else answer, OK? I am asking for help and have no intent to waste your time on trivia. Thanks class Point2d{ public: Point2d() {} Point2d(double x, double y) : X(x), Y(y) {} double x() const { return X; } double y() const { return Y; } /** * Returns the norm of this vector. * @return the norm */ double norm() const { return sqrt( X * X + Y * Y ); } void setCoords(double x, double y) { X = x; Y = y; }
*i am also new to C++* i'm not able to figure out where are X and Y defined i mean the capital ones.
-
*i am also new to C++* i'm not able to figure out where are X and Y defined i mean the capital ones.
Ratul Thakur wrote:
i'm not able to figure out where are X and Y defined
They aren't. The OP's code is incomplete and thus will not compile.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles