Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Point class in c++

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

  • Publicly derive from std::pair and add my own member functions? This is nice because all my code can stay the same. I am not going to be doing anything like std::pair<double, double>* p = new Point; so I don't have to worry about things like virtual destructors.
  • Roll my own Point class, which is annoying since I am duplicating std::pair's functionality, however I am "doing it the pure way".
  • Make template specializations of operator+ and operator- for std::pair, which admittedly I don't remember if they go in source or header files.

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.

like image 239
rlbond Avatar asked Apr 30 '09 22:04

rlbond


People also ask

How do you create a Point class in C++?

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.

Is it valid to create pointers that Point to 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.

Can you have a class in C?

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

What is class in C with example?

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.


2 Answers

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.

like image 126
Soo Wei Tan Avatar answered Oct 14 '22 20:10

Soo Wei Tan


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.

like image 44
leander Avatar answered Oct 14 '22 18:10

leander