How to Chage & operator

closed account (DEhqDjzh)
hi, The & operator returns the address of the variable. What I want to do is change & operator to something like _ or é or $ (You got the point). How can I do that?
default:
1
2
3
int *p;
int a;
p = &a 

That I want to do
1
2
3
4
int *p;
int a;
p = _a // or p = éa 
 
Operator overloading is a per-class thing, and you can only overload existing operators, not make new ones. the symbols you want are not existing operators.

~ or ! can be used but why bother, its already &. & is universally understood, does not need a class wrapper, and is the same amount of ugly,typing, and so on. Nothing to be gained, just confusion and clunk if you do this. Also, overloaded operators sometimes get their order messed up, the defaults have a known and well understood ordering.

that said, you can make a function with those names, or some of them. I hate macros but the loss of the type info in a macro might make this a good candidate. Though it still suffers the issue of confusing the reader and bloat.

void* _(type x)
{
return &x;
}

p = _(z); //sorta what you asked...
Last edited on
What I want to do is change & operator to something like _ or é or $ (You got the point).

Why?

If you are concerned about being able to freely change the meaning of operator&, use std::addressof consistently where the address of an object is actually required.
https://en.cppreference.com/w/cpp/memory/addressof

P.S.: I've noticed that you don't usually respond to questions, but you should, because the answers are important for understanding and fixing your problem.
I've noticed that you don't usually respond to questions

If he doesn't answer your question then we should stop answering his questions.
Last edited on
My impression is that this poster isn't really looking to learn and understand things. Their MO here seems to be to ask vague questions in the hope of getting someone else to write their code for them. They've certainly never engaged with any of the questions I've tried to ask them, or with any of the discussions I've tried to have with them.
closed account (DEhqDjzh)
@mbozzi Hi, Its just curiosity I wanted to create a custom operator
Topic archived. No new replies allowed.