PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How to accept both modifiable and non-modifiable arguments?

 July 06, 2022     c++, pass-by-reference, reference     No comments   

Issue

I have a debugging macro where I give it a float reference, and expect it to modify that variable sometimes, if it can.

#define probe(x) implProbe(#x##GETLINE, (x))

void implProbe(const char * loc, float & x){
    // this signature is a place holder
    ...
    x = 4.f;
}

However, I would also like to use the same macro for temporary variables or literals such as probe(1 + 2) or probe(x + y). The macro wouldn't need to have the same effect in those cases, I don't expect to see an output from it, I only want it to not break.

float var = 0.f;
probe(var);
// var == 4.f

var = 0.f;
probe(var + 2.f);  // this should be legal
// var == 0.f (didn't change)

probe(1.f); // this should be legal

Is there a way to accomplish this?


Solution

Implement two overloaded functions.

void implProbe(const char * loc, float & x){
    ...
    x = 4.f;
}
void implProbe(const char * loc, const float & x){
    ...
}


Answered By - 273K
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing