explicit conversion needed using cast operators?

(This is a continuation of a previous thread: http://www.cplusplus.com/forum/general/275419/)

1
2
3
4
5
6
7
8
9
10
11
12
13
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() 
	


This is great. But, now:

1
2
3
4
5
6
7
8
9
class Bar {
public:
   Bar(int);

};
foo f;
f = 42;
Bar *b = new Bar(f); // won't compile
Bar *bb = new Bar((int)f); // will compile, and use 42 as the arg.  


So, my question is: why wouldn't the compiler implicitly convert f to an int - which would call my cast operator (as in the "bb" case)?

TIA as always,

ken



So, my question is: why wouldn't the compiler implicitly convert f to an int


You should find that
f = 42;
does not compile, while
Bar *b = new Bar(f);
Is acceptable.

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

Even if it did compile, this would not be the behavior (unless f.m_i was 42 beforehand) because the expression f.operator int() is a prvalue. Assigning to it would modify a materialized temporary object, not f.

Please provide a complete example of the code that doesn't compile.
Last edited on
(This is a continuation of a previous thread: http://www.cplusplus.com/forum
/general/275419/)


That link isn't valid??

This compiles OK:

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
26
27
28
29
30
31
32
33
#include <iostream>

class foo {
public:
	explicit foo(int i_) : m_i(i_) {}
	operator int() const { return m_i; }

private:
	int m_i {};
};

class Bar {
public:
	explicit Bar(int b) : m_b(b) {}

	friend std::ostream& operator<<(std::ostream& os, const Bar& b)
	{
		return os << b.m_b;
	}

private:
	int m_b {};
};

int main()
{
	foo f {42};
	Bar* b {new Bar(f)};
	Bar* bb {new Bar((int)f)};

	std::cout << "b " << *b << '\n';
	std::cout << "bb " << *bb << '\n';
}

Okay. Sorry guys. I don't know why this didn't compile before. Actually, it was in another IDE, probably with different compiler settings. But, this is what I wanted, and this does compile and work:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

class foo {
public:
    foo() : m_i(0), m_v(nullptr) {};
    virtual ~foo(){};
    ;

    foo& operator=(int i)
    {
        m_i = i;
        return *this;
    };
    foo& operator=(void* v)
    {
        m_v = v;
        return *this;
    };

    operator int() { return m_i; };
    operator void *() { return m_v; };

protected:
    int m_i;
    void *m_v;
};

void somefunc(int i, void* v)
{
    std::cout << "i = " << i << '\n';
}

int main(int argc, char **argv){
    foo f;
    void *pv=nullptr;

    f = 42;
    f = pv;

    somefunc(f, f);

};



i = 42




Before, the compiler complained about no conversion from 'class foo' to 'int' (or void*). Like I said, don't know why it works now.

Thanks for the responses, tho.

ken
Topic archived. No new replies allowed.