2017-12-16 7 views
-2

Ich versuche, Vorlage polynomische Klasse mit überladenem Operator "+" zu erstellen. Ich habe es geschafft, dies mit Objekten arbeiten zu lassen, die auf den gleichen Variablentypen basieren (int + int), aber jetzt bin ich mit der Anpassung daran beschäftigt, mit Objekten basierend auf verschiedenen Arten von Variablen zu arbeiten (float + int). Ich möchte wählen, welche Art von Ergebnis ich basierend auf Arten von Polynomen bekommen werde, die ich summiere. Etwas wie dieses:Binärer Operator überladen

float + int -> float; 
float + double -> double; 
int + int -> int; 
unsigned int + int -> int; 

Und so weiter.

Im Moment i folgenden Code haben:

template <class ScalarType> 
    class Polynomial { 

    public: 
     Polynomial<ScalarType> operator+ (const Polynomial<ScalarType>& other) const; 

} 

template <class ScalarType> 
Polynomial<ScalarType> Polynomial<ScalarType>::operator+ (const Polynomial<ScalarType>& other) const { 
    bool firstIsMore = this->size() > other.size(); 
    Polynomial<ScalarType> result(firstIsMore ? *this : other); 

    typename std::vector<ScalarType>::iterator resultIt = result.nc_begin(); 
    typename std::vector<ScalarType>::const_iterator summIterBegin = (firstIsMore ? other.begin() : this->begin()); 
    typename std::vector<ScalarType>::const_iterator summIterEnd = (firstIsMore ? other.end() : this->end()); 

    while (summIterBegin != summIterEnd) { 
    *resultIt += *summIterBegin; 
    resultIt++; 
    summIterBegin++; 
    } 

    return(result); 
} 

Und das ist mein Versuch neccesary Funktionalität

template <class OtherScalar> 
    Polynomial<ScalarType> operator+ (const Polynomial<OtherScalar>& other) const; 


template <class ScalarType> 
class Polynomial { 

public: 

    template <class OtherScalar> 
    Polynomial<ScalarType> operator+ (const Polynomial<OtherScalar>& other) const; 

} 

template <class ScalarType> 
template <class OtherScalar> 
Polynomial<ScalarType> Polynomial<ScalarType>::operator+ (const Polynomial<OtherScalar>& other) const { 

    std::vector<ScalarType> summResult = this->getCoefficients(); 
    std::vector<OtherScalar> toSumm = other.getCoefficients(); 

    std::transform(summResult.begin(), 
       summResult.end(), 
       toSumm.begin(), 
       summResult.begin(), 
       [](const ScalarType& first, const OtherScalar& second){return (first + second);}); 

    if (summResult.size() < toSumm.size()) { 
    summResult.insert(summResult.end(), toSumm.begin() + (toSumm.size() - summResult.size()), toSumm.end()); 
    } 

    return(Polynomial(summResult)); 
} 

zu schaffen, aber wenn ich diese verwende ich Polynominal wird erhalten basierend auf dem ersten Typ ist ein binär Ausdruck, und das nicht, was ich brauche.

Abschlussfrage: Ist es möglich, einen binären Operator zu erstellen, der das Ergebnis basierend auf Operandentypen zurücksendet, ohne die Reihenfolge zu berücksichtigen. (Da es mit einfachen nummerische Typen funktioniert es möglich ist, aber ich habe keine Ahnung, wie diese Arbeit zu machen)

Ich Speicherung Koeffizient polynominal in std::vector<ScalarType> Here ist voll Klassencode

+2

getestet Hat den ganzen Beitrag nicht lesen, aber man kann feststellen, [ 'std :: common_type'] (http: //en.cppreference .com/w/cpp/types/common_type) nützlich. – StoryTeller

Antwort

1

Ich nehme an decltype() könnte dir helfen.

So etwas wie

template <class OtherScalar> 
Polynomial<decltype(ScalarType()+OtherScalar())> operator+ 
    (const Polynomial<OtherScalar>& other) const; 

ps: Vorsicht: nicht