-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vector3 object #7
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work! first round of reviews!
} | ||
], | ||
"version": 4 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file should probably not have made it into the repo, right?
course/include/Vector3.h
Outdated
@@ -0,0 +1,49 @@ | |||
#ifndef VECTOR3_H | |||
#define VECTOR3_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can now use #pragma once
instead of using guard macros. https://en.wikipedia.org/wiki/Pragma_once
course/include/Vector3.h
Outdated
public: | ||
explicit Vector3(double x, double y, double z) : x_(x), y_(y), z_(z) {} | ||
explicit Vector3() : x_(0.0), y_(0.0), z_(0.0) {} | ||
double x() const { return x_; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better return a const double &
and avoid an additional copy. For doubles is probably not that important, but if it where a larger variable type it can improve performance.
course/include/Vector3.h
Outdated
explicit Vector3() : x_(0.0), y_(0.0), z_(0.0) {} | ||
double x() const { return x_; } | ||
double y() const { return y_; } | ||
double z() const { return z_; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to create updatable versions of these functions; that is, versions that return non-const lvalue references, so that you can write vector.x() = 3.0;
.
course/include/Vector3.h
Outdated
double dot(const Vector3 &q) const; | ||
Vector3 cross(const Vector3 &q) const; | ||
//operator overload | ||
const double& operator[](int index) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make the index parameter be const
.
std::stringstream ss; | ||
ss << p; | ||
EXPECT_EQ(ss.str(), "(x: 1, y: 2, z: 3)"); | ||
EXPECT_TRUE(Vector3::kUnitX == Vector3(1., 0., 0.)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to add this line.
course/test/src/isometry_TEST.cc
Outdated
std::stringstream ss; | ||
ss << p; | ||
EXPECT_EQ(ss.str(), "(x: 1, y: 2, z: 3)"); | ||
EXPECT_TRUE(Vector3::kUnitX == Vector3(1., 0., 0.)); | ||
|
||
EXPECT_TRUE(Vector3::kUnitX != Vector3(1., 1., 0.)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better make them an initializer list, and implement equality between vectors and initializer lists.
course/test/src/isometry_TEST.cc
Outdated
t[1] = 2.; | ||
t.z() = 3.; | ||
t[2] = 3.; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was ok the way it was. You need to implement a z()
overload that returns an lvalue reference so that you can use it on the left side of assingments.
course/test/src/isometry_TEST.cc
Outdated
EXPECT_EQ(t, p); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to add this line.
} // namespace test | ||
} // namespace cppcourse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably we don't need another namespace layer, but I'll leave it up to you.
…12020 into Vector3Object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work! Second round of review done.
@@ -0,0 +1,2 @@ | |||
/build | |||
/.vscode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with this, but keep in mind that usually is not a good idea to update the .gitignore file in the clients repo to ignore files in your own personal computer setup.
class Vector3 | ||
{ | ||
public: | ||
explicit Vector3(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to do this explicit.
explicit Vector3(const double x, const double y, const double z); | ||
Vector3(const std::initializer_list<double> &list); | ||
|
||
double x() const {return values[0];} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This is fine, but you could instead return a const double &
, which will in general avoid an additional copy. For double
is not really important, but for larger objects it may be.
double x() const {return values[0];} | ||
double &x() {return values[0];} | ||
|
||
double y() const { return values[1]; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
double y() const { return values[1]; } | ||
double &y() {return values[1];} | ||
|
||
double z() const { return values[2]; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here.
{ | ||
return values[index]; | ||
} | ||
catch (const std::exception &ex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test for this case in the test file.
{ | ||
return values[index]; | ||
} | ||
catch (const std::exception &ex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add a test for this case.
|
||
private: | ||
|
||
double *values = new double[3]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't release this memory somewhere, this memory will get leaked everytime you create one of these objects.
{ | ||
public: | ||
explicit Vector3(); | ||
explicit Vector3(const double x, const double y, const double z); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a copy constructo.
{ | ||
public: | ||
explicit Vector3(); | ||
explicit Vector3(const double x, const double y, const double z); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apply the rule-of-five and create move operations: construct and assign. (this is why I wanted an allocated storage).
No description provided.