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?

3
  • 5
    Possible duplicate of What is an "operator int" function? Commented Nov 3, 2017 at 8: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) Commented Nov 3, 2017 at 8: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. Commented Nov 3, 2017 at 13:47

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

int() is cast-operator () ?
@HoànTrần It is a conversion operator that can be used in casting constructions. You can make it either implicit or explicit as it is shown in the answer.
can not overloading operator = for casting?
@HoànTrần You may not overload the assignment operator for fundamental types.
@HoànTrần You may overload only a compound assignment operator outside a class definition for fundamental types when one operand is a user defined type.
|
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; }; 

Comments

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 =)

3 Comments

I want 2 classes can use equivalent, almost, call through the function is clear but annoying.
@HoànTrần : You need a lot more motivation explaining what you are trying to do in order to explain why your case is an exception to the rule. I'm not sure SO is the right place to do that.
I want overloading assignment operator, not cast operator. But it seems impossible to do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.