简体   繁体   English

模板运算符重载基类

[英]template operator overload base class

I have a base class with many child classes. 我有许多子班的基础班。 How would I implement a template operator over loader in the base class to work for all the inheriting classes? 如何在基类中的加载程序上实现模板运算符,以便为所有继承的类工作? I tried to make one with the + operator but it complained that I had too many parameters. 我试图用+运算符来写一个,但是它抱怨我参数太多。 I'm not actually sure this is the right way to go about doing this (I'm just starting to use OOP) so if you can think of a better way that would also be great. 我实际上不确定这是执行此操作的正确方法(我刚刚开始使用OOP),因此,如果您能想到一种更好的方法也将是个不错的选择。

I'm making a library in which each metric space is a class. 我正在建立一个库,其中每个度量空间都是一个类。 I want to make a base class "Operations" that every space inherits. 我想创建每个空间都继承的“操作”基类。

My template base class: 我的模板基类:

#ifndef __libSpace__Operations__
#define __libSpace__Operations__

template< typename T >
class Operations{
public:
    friend T operator+( const T& sp, const T& nsp ){
        return T(sp.dimension + nsp.dimension);
    };
};

#endif

child: 儿童:

#ifndef __libSpace__EuclidSP__
#define __libSpace__EuclidSP__

#include "Operations.h"

class EuclidSP: public Operations<EuclidSP>{
public:
    EuclidSP(int n = 0, ...);
    ~EuclidSP();

    double* vector();

private:
    int dimension;
    double *vec = new double(dimension);
};

#endif

main: 主要:

#include <iostream>
#include "EuclidSP.h"

int main(int argc, const char * argv[])
{
EuclidSP ob1(3,4.0,5.0,6.0);
EuclidSP ob2(3,2.0,5.0,3.0);
EuclidSP obj3();

obj3 = ob2+ob1;

return 0;
}

A member operator +() have only one parameter, the right operand. 成员operator +()只有一个参数,即右操作数。 The left or first is always *this . 左边或第一个始终是*this Depending of your situation, you need just a base + , a virtual + or a template. 根据您的情况,您只需要基本+ ,虚拟+或模板。 A free operator +() take two argumets, "left" and "right". 一个自由operator +()带有两个参数,“ left”和“ right”。

In your code: 在您的代码中:

template< typename T >
class Operations{
public:
    friend T operator+( const T& sp, const T& nsp ){
        return T(sp.dimension + nsp.dimension);
    };
};

You whant a member or a friend? 您想成为会员还是朋友?

If a friend, the problems is that +() have to be define outside the class, it is only a friend, not a member. 如果是朋友,问题在于+()必须在类之外定义,它只是朋友,而不是成员。

template< typename T >
    T operator+( const T& sp, const T& nsp );

template< typename T >
class Operations{
public:
    friend T operator+<T>( const T& sp, const T& nsp );

};

template< typename T >
    T operator+( const T& sp, const T& nsp )
    {
        return T(sp.dimension + nsp.dimension);
    }

BUT !!!! 但是!!!! Now you have the REAL problem: +() uses a privat member of the derived class, not the base class, so it need to be a friend of the derived class. 现在您遇到了真正的问题:+()使用派生类的私有成员,而不是基类,因此它需要成为派生类的朋友。 I think you need to rethink ;-) your design. 我认为您需要重新考虑;-)您的设计。 If you are so confortable using dimension in Operations.. could it be a protected member of Operations??? 如果您在Operations中使用维度非常方便,那么它是否可以作为Operations的受保护成员??? All your Operations have an dimension? 您所有的运营都有维度吗?

Your base class should know the derived types class by receiving it as a template parameter and implement operator+ as friend : 您的基类应该通过接收派生类型类作为模板参数来知道派生类型类,并将operator+实现为friend

template< typename T >
class base
{
    friend T operator+( const T& lhs, const T& rhs )
    {
       // your generic implementation here...
    }
};

the derived classes then derive from the base class like this: 然后,派生类从基类派生如下:

class D1 : public base<D1> { ... };
class D2 : public base<D2> { ... };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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