Right now I am using std::pair to represent a 2d point in c++. However, I am getting annoyed with having to write
typedef std::pair<double, double> Point;
Point difference = Point(p2.first - p1.first,
p2.second - p1.second);
instead of being able to overload operator+ and operator-.
So, my question is, to make my Point class, should I
std::pair<double, double>* p = new Point;
so I don't have to worry about things like virtual destructors. I guess it's up for debate, I'd really like to do #1 but I don't know if it's a bad idea since I've heard that inheriting from STL is a no-no.
Implement the Point and Line Class in C++ Start with a header file that includes all the necessary declarations. Create a Point class that contains two data members, x and y . Create a Line class that contains two data members, startPoint and endPoint . Define the constructor for both classes.
"It is perfectly valid to create pointers that point to classes. We simply have to consider that once declared, a class becomes a valid type, so we can use the class name as the type for the pointer" isn't terribly accurate writing.
No, C has no classes per se, only C++ (which started out as "C with classes" back then...). But you can use the standard C library in C++ code, even if it is often not considered good practice (where C++ has its own, higher level constructs, e.g. cout vs printf ).
1. C Classes. A class consists of an instance type and a class object: An instance type is a struct containing variable members called instance variables and function members called instance methods. A variable of the instance type is called an instance.
You could roll your own Point class, but use std::pair internally to store the data. This prevents the inheritance from STL issue, but still uses std::pair's functionality.
Better than rolling your own: grab an existing free Vector/Point library. One recommendation: the one attached to Essential Math for Games Programmers. You can use whatever library you find as a starting point, then optimize / specialize / tweak from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With