简体   繁体   中英

operator= overload in C++

I need some help with operator assignment. This is code:

Sinusoid.h:

class Sinusoid : public Component
{   
    float fs, f, A, fi;
    int N;
    double *array;

public:
    Sinusoid(float fs, float f, float Ts, float fi, float A);
    void count(int type=1);
    void clear();
    ~Sinusoid();
    double *getArray() { return this->array; }
    double*& operator=(Sinusoid* const &rhs) {
        return rhs->array;
    };
};

main.cpp:

#include "headers.h"
int main()
{
    int N = 1000 * 2.5;
    Sinusoid *sinus = new Sinusoid(1000, 15, 2.5, (M_PI / 4), 0.7);
    double **answers = new double*[7];
    for (int i = 0; i < 7; i++) {
        answers[i] = new double[N];
    }
    //lab1
    //Zad1
    sinus->count(1);
    answers[0] = sinus;
    return 0;
    }

When i build this code i've got the following problem:

C2440 '=': cannot convert from 'Sinusoid *' to 'double *' main.cpp:15

I know that i can assign two classes with overloaded operator "=" but i want to take a private member of class (double *array;). I know that i can do it by "getArray()" method but i want to learn more "beautiful" practice. Hope you will help me.

Thank you.

Your double*& operator=(Sinusoid* const &rhs) operator doesn't do what you apparently think it does.

An operator= member function allows you to have an instance of the class ( Sinusoid ) on the left side of an assignment and an instance of the parameter type ( Sinusoid* ) on the right side. So what you wrote would let you do:

Sinusoid* pointer = whatever;
Sinusoid object;
object = pointer;

Obviously that's not what you intended. (And probably not something you'll ever want.)

The return type of the operator= doesn't have any influence on this fundamental usage. All it means is that the result of object = pointer is then a double*& . So it would let you write:

double* foo;
foo = (object = pointer);

And that's still not helpful for what you want.

Since answers[0] is a pointer, there's no way to create a custom assignment operator for it. However the language has a different mechanism which can be used here. You can create an implicit conversion operator for your class so that compiler is allowed to convert/decay it into a different type when reasonable. Try adding the following to your class:

operator double* () {
    return rhs->array;
}

That should work for the case you want.

Although personally I don't think there's anything wrong with leaving the usage to require calling getArray() in the first place. Sometimes the clarity of an explicit function call can make it easier to read what's actually going on.

Try...

class Sinusoid : public Component {
...
   operator double*()  {
       return array;
   }

and the usage

answers[0] = *sinus;

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