464

How to cast a variable member to pass it as reference argument of a function

Question:

When I try to compile some code very similar to this one with VC++2015 I get a:

<strong>C2664 cannot convert parameter number 1 from 'unsigned int' to 'short &'</strong>

class Foo { public: unsigned int A; unsigned int B; } void foo(short& a) { a++; } void main() { Foo f; foo(f.A); }

What is the correct way to cast it?

Answer1:

It is not possible to do this with a cast because unsigned int cannot be aliased as short. To call this foo without changing it, the code would be:

if ( f.A > SHRT_MAX ) throw std::runtime_error("existing value out of range for short"); short sh = f.A; foo(sh); f.A = sh;

You may want to check sh >= 0 before reassigning it to f.A; and foo should guard against integer overflow.

Recommend

  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Why does the following throw an “Object doesn't support property or method 'importNode
  • AWS-SES: Handling Bounces for Invalid ISPs
  • Why doesn't a local variable live long enough for thread::scoped?
  • Django model for a Postgres view
  • How to retrieve information from antrun back to maven?
  • How do I signal completion of my dataflow?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • how to avoid repetitive constructor in children
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Spark fat jar to run multiple versions on YARN
  • Meteor helpers not available in Angular template
  • Avoid links criss cross / overlap in d3.js using force layout
  • Possible to stop flickering java tooltip in heavyweight mode?
  • output of program is not same as passed argument
  • MySQL WHERE-condition in procedure ignored
  • Eraser for UIBezierPath
  • ActionScript 2 vs ActionScript 3 performance
  • Build own AppleScript numerical error handling
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Akka Routing: Reply's send to router ends up as dead letters
  • Circular dependency while pushing http interceptor
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • json Serialization in asp
  • Suggestions to manage Login/Logout transitions
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?