overload operator '='

I have this:

class foo {
public:
foo(){};
~foo(){};

void* operator= (foo& f){return m_p;}; //getter
foo& operator=(void* p){m_p = p; return *this}; //setter

protected:
void *m_p;

};

in my code:

foo f;
char* str="test";
void* v;

f = (void*)str; // sets m_p to str
v = f; // doesn't compile: incompatible type (void * from foo)

v = f.operator=(f); // sets v to str

Am I missing something here?

Am I missing something here?

Yes. You're missing code tags! :-)

[code]
Put your code in code tags to preserve indentation.
[/code]

Also, posting a complete program is always helpful.

Your operator=(foo& f) is wrong.
It doesn't even use f.
It should do: m_p = f.m_p and return *this

In essense, v = f in your code means: v.operator=(f)
Put the built in operator= for a void* does not know how to assign from a foo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

class foo {
public:
    foo& operator=(const foo& f) { m_p = f.m_p; return *this; };
    foo& operator=(void* p)      { m_p = p;     return *this; };
    void* get() const { return m_p; }
private:
    void* m_p = nullptr;
};

int main() {
    foo f;

    f = (void*)"test";
    void* v = f.get();
    std::cout << (char*)v << '\n';

    foo g, h;

    g = h = f;   // returning *this allows chaining

    std::cout << (char*)g.get() << '\n';
    std::cout << (char*)h.get() << '\n';
}

sorry about the code tags. And the missing code is from being lazy.

I get the get(), that is just a getter :-). Since the overload of '=' for a setter worked, I tried to make it work for the getter. But, I am a bit confused. How would you define the getter function as an 'operator=' overload?

Also, if I added an int member, then I can add another overload operator=(int) for the int setter , but can use the same function name get() to return the int?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class foo {
public:
    foo& operator=(void* p)      { m_p = p;     return *this; };
    foo& operator=(int i)      { m_i = i;     return *this; };
 
    void* get() const { return m_p; }
    int get() const {return m_i;}; // can't redefine "get()"

private:
    void* m_p = nullptr;
    int m_i=0;
};

int main() {
    foo f;

    f = (void*)"test";
    void* v = f.get();
    f = 1;
    int i=f.get();   // bad..
    printf ("v=%s\ni=%d\n", (char*)v, i); 

 }


Probably should use templates at this point?

operator= is the "assignment operator", i.e., it is a "setter", so it would be very bad form indeed to define it as a "getter". Think about it. It doesn't make any sense at all, since you would use it something like x = y, which calls operator= on the x object, passing the y object: x.operator=(y). But you wouldn't even be using the y object since you just want to return a member of x. It's insane!

As for your get() functions, you can't overload a function on it's return type. You can just give them separate names, getInt(), getPtr().

Alternatively you could possibly use conversion operators:

1
2
3
4
5
6
7
8
9
10
11
12
class foo {
public:
    ...
    operator void*() const { return m_p; }
    operator int()   const { return m_i; }
    ...
};

...
foo f;
f = 42;
int n = f;  // n is 42 because of operator int() 

That is exactly what I need. It will make using this class much cleaner and easier.

2 thumbs up!

Thanks.
Topic archived. No new replies allowed.