- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a C++ function called by Fortran which passes arguments by reference. The parameters in the C++ function are reference types (int& var) and the program compiles and runs without error. I can't use value types for the dummy arguments, whereas in C++ the following value assignment to a reference is legal:
int var = 0;
int &var = var;
All examples I have found which describe passing by reference use C-style pointers in C/C++ interfaces. Is the use of reference types discouraged (presumably so in case a null argument is passed)?
Thanks.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
All examples I have found which describe passing by reference use C-style pointers in C/C++ interfaces. Is the use of reference types discouraged (presumably so in case a null argument is passed)?
In one word -- no. I use references all the time in C++ (well, every time they're viable), and interface them with Fortran heavily. I suppose pointers are a heritage of C proper in this area, but you should be just fine with references.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Jim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks - I'll continue to use references.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In all implementations I'm aware of C++ references are implemented as a C pointer behind the scenes, though I've not found a formal requirement for that in the C++ standard (I'm no expert here - if someone knows better, please point me to it). Practically, you can therefore assume that references and pointers "interoperate". I guess there's always the infinitessimally small chance that a compiler vendor with a pathological bent might decide to trip you up one day but I wouldn't lose any sleep over it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
On one side of the fence you could concievably have an interpreter (not the original poster's case) where the interpreter uses token references (e.g. an index or key or name) that is not a pointer. While the code on the interpreter side could reach the data by way of the token, the other side (non-interpreter) could not. Explicitly using pointers on the C (C++) side could avoid a portability issue by explicitly stating the argument is "address of".
Pointers can get you in trouble too. e.g. a non-SMP system where processors may not necessarily have the same word length or other properties (different address space such as on Harvard archetecture).
Jim Dempsey
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page