Replacing Operators

Thread Starter

ElectricSpidey

Joined Dec 2, 2017
2,774
Is it possible to replace an operator with a variable in C++?

In other words instead of…

++ Foo;

Could I use…

int var = ++;

Then…

var Foo;
 
Last edited:

Thread Starter

ElectricSpidey

Joined Dec 2, 2017
2,774
Because... :p

I need to change a PWM value to a new value using a loop (step), but I wont know whether I need to increment or decrement the value until its ready to change, so I was hoping I could change the operator as needed after determining the direction.
 

WBahn

Joined Mar 31, 2012
30,045
Because... :p

I need to change a PWM value to a new value using a loop (step), but I wont know whether I need to increment or decrement the value until its ready to change, so I was hoping I could change the operator as needed after determining the direction.
Even if you could do what you want, what has it bought you? Once you know the direction you would still need to change the code and recompile.

There are languages where you can do this, such as lisp.

Why not just do something like:

int step = 0;

// Code to set step to some value such as +1 or -1

Foo += step;

Another way would be

// Code to set dir to 0 for decrement, nonzero to increment

dir? Foo++ : Foo--;
 

AlbertHall

Joined Jun 4, 2014
12,346
Because... :p

I need to change a PWM value to a new value using a loop (step), but I wont know whether I need to increment or decrement the value until its ready to change, so I was hoping I could change the operator as needed after determining the direction.
You could set var to either plus or minus 1, then do 'foo + var' which will either increment or decrement foo.
 

nsaspook

Joined Aug 27, 2009
13,265
IMO purely from a structured programming viewpoint your desired capability seems to Obfuscate instead of Clarify the intent of the code and as others have said here there are much better ways to express what you want without the possible runtime hit needed to decode variable capabilities beyond memory operation requirements.
 

WBahn

Joined Mar 31, 2012
30,045
So the bottom line is, changing a operator on the fly is a big no-no.
It depends.

This is almost always a correct answer to any engineering-related question.

You can't do it like you are describing in C++. There are languages where you can. Those languages tend to suffer from poor runtime performance because of all the extra overhead associated with doing what you are trying to do.

Whether it is a big no-no depends entirely on what is important to you.

You haven't given a hint as to what is important to you and what you might be willing to trade to achieve it, so the best we can do is tell you that it depends.
 

Thread Starter

ElectricSpidey

Joined Dec 2, 2017
2,774
When it comes to coding what is important to me is...doing the job and keeping it simple. I'm not married to any particular way of doing something but with my lack of experience it can take quite a long time to come up with any method, so I need to find out issue by issue which ones work and which ones wont.

And I thank you all.
 

MrChips

Joined Oct 2, 2009
30,795
When it comes to coding what is important to me is...doing the job and keeping it simple. I'm not married to any particular way of doing something but with my lack of experience it can take quite a long time to come up with any method, so I need to find out issue by issue which ones work and which ones wont.

And I thank you all.
That's what we're for.
Don't be afraid to ask.
 
Top