Skip to content
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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Vector3 object #7

wants to merge 19 commits into from

Conversation

patapufate
Copy link

No description provided.

Copy link
Owner

@glpuga glpuga left a 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
}
Copy link
Owner

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?

@@ -0,0 +1,49 @@
#ifndef VECTOR3_H
#define VECTOR3_H
Copy link
Owner

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

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_; }
Copy link
Owner

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.

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_; }
Copy link
Owner

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;.

double dot(const Vector3 &q) const;
Vector3 cross(const Vector3 &q) const;
//operator overload
const double& operator[](int index) const;
Copy link
Owner

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.));

Copy link
Owner

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.

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.));
Copy link
Owner

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.

t[1] = 2.;
t.z() = 3.;
t[2] = 3.;
Copy link
Owner

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.

EXPECT_EQ(t, p);

Copy link
Owner

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
Copy link
Owner

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.

Copy link
Owner

@glpuga glpuga left a 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
Copy link
Owner

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();
Copy link
Owner

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];}
Copy link
Owner

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]; }
Copy link
Owner

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]; }
Copy link
Owner

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)
Copy link
Owner

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)
Copy link
Owner

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];
Copy link
Owner

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);
Copy link
Owner

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);
Copy link
Owner

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants