🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ tutorials

Started by
93 comments, last by taby 1 year, 9 months ago

They way you implemented equals and less than operators brings to mind you don't mention how logical expressions and bitwise logical expression work in C++. I would also take about freestanding operator overloading btw in your operator overloading chapter.

bool operator < (const A& lhs, const A& rhs)
{
	return lhs.x < rhs.x && lhs.y < rhs.y
}

bool operator==(const A& lhs, const A& rhs)
{
	return lsh.x == rhs.x && lhs.y == rhs.y;
}

They can become super handy when you have to overload things you can't express with a member overload operator.

Vector2 myVector; //Vector has a Vector2 operator*(float scalar);

auto result = myVector * 2;
auto result = 2 * myVector; //you can't express this with a member function for example.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Advertisement

These are excellent points. I will implement them tonight or tomorrow. Your input is greatly appreciated.

Also its generally easier to read a less than opertor that doesnt do a greater than operator to calculate the result. That might require the reader to do more mental work than needed in a few places. This works the same for conditional code its much easier to read the positive case instead of the negative case in an if expression specially when the logic expression becomes complicated. ALso that “!” in front of an expression can be hard to spot.

When you work on large codebases all these small things matter in how much energy you have to expend to understand code which matters when you look at a lot of code.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

I have a strong hunch that the Eigen library overloads the operators like you are suggesting. Very handy. Thanks again.

#include "Eigen/Dense"
using namespace Eigen;

...

MatrixXf m(3, 3);
m << 5, 6, 7, 8, 9, 10, 11, 12, 13;
	
MatrixXf vec_m = 2 * m; // Oh yeah, baby!
VectorXf m_vec = m * 2;

cout << vec_m << endl;
cout << m_vec << endl;

Ok, so a scalar times a vector is a matter of multiplying the scalar by the vector‘s components.

Is a vector times a scalar the same thing? I guessed not. Now I convert the scalar into a vector by setting the y and z components to zero, and multiplying that using the cross product. is this the correct way to do things?

taby said:
Is a vector times a scalar the same thing? I guessed not. Now I convert the scalar into a vector by setting the y and z components to zero, and multiplying that using the cross product. is this the correct way to do things?

Vector times scalar or scalar times vector is the same thing. The multiplication is cumulative, and there is no cross product involved.

Sounds good! Thanks, man.

Yeah, my boss pointed out that they are indeed equal when computing them using Wolfram Alpha. /facepalm

I tried making the vertex's member variables protected. I found that in order to do this, one must also mark the operator overloads as friend functions. This is correct usage, right?

https://github.com/sjhalayka/sample_cpp_code/blob/main/03_advanced/s21_overloading_operators.cpp

Unless you have accessors or differ to other functions in the public API. I dislike friend but yes this is a valid use for it.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement