简体   繁体   中英

How to inherit the functionality of overloaded operators in C++

So I have a point class that represents a point in n-dimensional space, and a vector class that inherits from the point class and adds some extra functionality specific to vectors, such as dot products. I want the vector class to inherit the overloaded operators (+, - (unary), - (binary), etc.) from the parent point class. In the original python, I defined the __add__ operator as such:

def __add__(self, other): # should be same dimensionality
    sum = self.__class__.zeros(self.dimension())
    for i in range(self.dimension()):
        sum[i] = self[i] + other[i]
    return sum

So it takes self and another object (presumably of the same type), calls a method of the class that generates another point/vector with the same number of dimensions/components and all values of zero, and then fills that point/vector with the added components and returns it. In the python, my vector class inherits this method as is from the point class, and there is no problem because it creates another object of the same class by using self.__class__ to call zeros (which itself calls the classes constructor with zero values for every argument). I have a C++ version, which looks like this:

Point operator+(const Point other) const { // dimensionality must match
    vector<long double> new_comps(_comps.size(), 0);
    for (size_t i(0); i < _comps.size(); i++) {
        new_comps.at(i) = _comps.at(i) + other._comps.at(i);
    }
    return Point(new_comps);
}

The problem is that in the C++, I used Point as the return type and called the constructor of the point class directly. Is there any equivalent to self.__class__.__init__ in C++? More broadly, is there any way to write a member function like this operator so that it calls the constructor of the current class and returns on object of that type, so that the generic function can be inherited and work analogously for each class that inherits it? Or is that sort of behavior not possible in C++, or should I use a different technique (composition instead of inheritance) for this problem? As it is, my vector class inherits from the point class and adds its own member functions just fine, but its + operator returns a point instead of a vector, which is problematic. (To be clear, throughout this I have been referring to the mathematical object of a vector, no the C++ STL container of a vector, though I use that in my implementation.)

Ignoring some semantic differences (like is a vector a point), no. What you typically would do instead is make a static Point zeros() method that returns a zero'd form of the class, and then call that from the operator overload. If each sub-class implements the method, than it will correctly return the expected type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM