0

As title, can be to overloading operator = for casting? I have a simple class.

    class A{
protected:
    int m_int;
public:
    A& operator=( int& obj)
    {
        m_int = obj;
        return *this;
    }
};

I want:

A t_a = 1;

and

int t_int = t_a;

Is there a way to do this?

Hoàn Trần
  • 99
  • 1
  • 9
  • 5
    Possible duplicate of [What is an "operator int" function?](https://stackoverflow.com/questions/3814865/what-is-an-operator-int-function) – Dean Seo Nov 03 '17 at 08:08
  • This is not necessarily what I need. Int is just an example. I want to be able to overload in the opposite direction, operator=(int& lhs, A& rhs) – Hoàn Trần Nov 03 '17 at 08:14
  • `operator=` cannot be a cast. It’s an assignment operator. A cast is something you write in your source code to tell the compiler to do a conversion. – Pete Becker Nov 03 '17 at 13:47

3 Answers3

2

Just define conversion operator

operator int() const
{
    return m_int;
}

or

explicit operator int() const
{
    return m_int;
}

In the last case you have to use an explicit casting in the statement

int t_int = int( t_a );

Take into account that the assignment operator should be declared like

A& operator=( const int& obj)
{
    m_int = obj;
    return *this;
}

or like

A& operator=( int obj)
{
    m_int = obj;
    return *this;
}

Otherwise it will be impossible to bind the non-constant reference with integer literals or temporary values.

As for the assignment operator then you may define only a compound assignment operator for the type int and the type A.

For example you could define the operator += or something other operator.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
A t_a = 1;

This doesn't use assignment. You need a constructor which takes an int argument.

int t_int = t_a;

You will need operator int() for this.

Note that it is a really bad idea to have a class which has both an implicit constructor from a type, and an implicit cast to the type. You will get all sorts of confusing errors when you try to do overload resolution.

Instead, I would make the constructor explicit, and write an explicit conversion function. That means you have to write:

int t_int = t_a.to_int();

But at least it's explicit.


Edit: Note that you can overload operator = for casting (either inside or outside the class), but neither of the code samples you gave will use it. = is used both for assignment and initialization, and both your samples are initialization (so won't use operator =)

0

Yes, that’s possible. You need a custom ctor and assignment operator. But writing those disables some of the compiler generated ctors/assignment ops. If you still need/want them, you need to reintroduce them explicitly.

class A
{
protected:
    // Note the initializer. Without it m_int is uninitialized
    // when you default construct an A. That’s a common source
    // of bugs.
    int m_int = 0;

public:
    // Following two are the important ones
    A(int i) : m_int(i) {}

    A& operator=(int i)
    {
        m_int = i;
        return *this;
    }

    // if A needs to be default constructible as well
    A() = default;

    // The int ctor/assignment disable the compiler generated
    // normal copy ctor/assignment (the ones that take another A).
    // Reintroduce them like this:
    A(const A&) = default;
    A& operator=(const A&) = default;

    // Writing any copy ctor/assignment disables the compiler generated
    // move ctor/assignment. If you still want them, reintroduce them.
    A(A&&) = default;
    A& operator=(A&&) = default;
};
besc
  • 2,507
  • 13
  • 10