Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34220
| From | Marcel Mueller <news.5.maazl@spamgourmet.org> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Need to get the variable name from the called method |
| Date | 2016-04-29 08:41 +0200 |
| Organization | MB-NET.NET for Open-News-Network e.V. |
| Message-ID | <nfuvms$ii5$1@gwaiyur.mb-net.net> (permalink) |
| References | <8735ea75-7793-49ad-a31d-ebf6c2691bac@googlegroups.com> |
On 28.04.16 17.16, saransamurai7@gmail.com wrote:
> class Test {
>
> public static void main(String args[]) {
>
> int valueInt = 10;
>
> function(valueInt);
>
> }
>
> void function(int param) {
>
> // Here I need to get the output as "valueInt" & not 10.
>
> }
>
> Kindly help me to get the variable name "valueInt" to be printed inside the method 'function'
First of all a C++ executable has no means of variable names. They are
just memory locations at this time. No more no less. So your entire
program does no longer know how your variables were called. The
information is lost during compilation.
You need to copy the variable name into your code as string literal.
Basically by writing function("valueInt");
Now it will no longer compile because the types do no longer match.
Of course, this is error prone because the compiler can no longer check
whether "valueInt" is a variable name or not.
Secondly if you want function() to print a string rather than an integer
it need to take a string as argument. I.e. change the definition to
void function(std::string param) {
This will resolve the type mismatch above.
C++ has no direct access to variable names. But the old C preprocessor
that is still part of the language can do something similar.
If you define a helper macro like
#define function_helper(x) function(#x)
Now you can write
function_helper(valueInt);
to get the desired string.
Since the preprocessor as the name suggests runs before the compiler it
will just transform your code into
function("valueInt");
So there is no more type checking as if you wrote the double quotes
yourself. So
function_helper(smörebröd);
will compile as well.
However, if you need both, the name and the value, the situation changes.
Now your function is defined as
void function(int value, std::string name) {
and your helper could be
#define function_helper(x) function((x), #x)
Since the value of x is now used it must evaluate the an integer. The
compiler will complain if you pass trash to function_helper because the
resulting call
function(valueInt, "valueInt");
need to be valid code.
But note what happens if you write.
function_helper(valueInt + 1);
It will result in
function(valueInt + 1, "valueInt + 1");
which is still valid code but maybe not what you intended.
Marcel
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Need to get the variable name from the called method saransamurai7@gmail.com - 2016-04-28 08:16 -0700
Re: Need to get the variable name from the called method Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-04-28 13:31 -0400
Re: Need to get the variable name from the called method Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-04-29 08:41 +0200
Re: Need to get the variable name from the called method Lew <lewbloch@gmail.com> - 2016-04-29 11:18 -0700
Re: Need to get the variable name from the called method Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-04-29 20:52 +0200
csiph-web