How can the func function * programmatically recognize the "name of the object" that calls it?

Let's say we have a class MyClass, which also has memberfunc (). An object is created for this MyClass, for example ObjA.

i.e MyClass ObjA;

ObjA calls memberfunc ().

Can we get this name "ObjA" inside memberfunc () programmatically?

Note. I know how to get the type of an object, i.e. "MyClass" using RTTI (runtime type identification), the same is explained by the radman below.

EDIT:

If this is NOT POSSIBLE in C ++, is this possible in any other programming language?

EDIT2

Made some change in the question, since few could not interpret.

+3
source share
7

:

  • , .
  • . , , - , .
  • , , , , .
    • Python . , . ++ .

++, (, , ) , , , , " ?" .

+8

, . ++ , . 2- , , Java .

++ , . , " " ( , ...).

+3

, - , . - . , - .

(, Reflection) . # - girly man - # 3.5, , , . - .

, - -, - . :

class ObjA {
public:
  void memberfunc() {
    //confused??? instance1 or instance2?
  }
};

//main
ObjA instance1;
ObjA* instance2 = &instance1;
instance2->memberfunc();

ObjA , ( , ). - , , .

# Reflection . , , - -, , . , , , Reflection.

public static class Extensions {
  public static string GetFirstPropertyName(this object obj) {
    return obj.GetType().GetProperties()[0].Name;
  }
}

public class Program {
  public static void Main() {
    int intVal = 5;
    var name = (new {intVal}).GetFirstPropertyName();
    //name=="intVal"
  }
}
+3

, , , -, .

, typeid. . , , , , (, , "4ObjA".

#include <iostream>
#include <typeinfo>
class ObjA
{
public:
  void memberfunc()
  {
    std::cout << typeid(*this).name() << std::endl;

  }
};

int main(int argc, char **argv)
{
  ObjA obj;
  obj.memberfunc();

}
0

- , ? , -? - ..?

- :

class Myclass(object):
    def memberfunc(self):
        print self.__class__.__name__

obja = Myclass()
obja.memberfunc() # prints Myclass

obja ( ), , ( - / )

, , , , Python, inspect:

import inspect

class Myclass(object):
    def memberfunc(self):
        current_call = inspect.stack()[0]
        previous = inspect.stack()[1]
        print previous[3]

def somefunc():
    obja = Myclass()
    obja.memberfunc() # prints somefunc

somefunc()

,

, , , , , ,

0

, , - . Ruby :

class Foo
   def foo
      puts self.class
   end
end

class Bar < Foo
end

f = Foo.new
b = Bar.new

f.foo #=> Foo
b.foo #=> Bar

++ typeid, . :

#include <iostream>
class Foo {
    public:
        void foo () { std::cout << typeid(this).name() << std::endl; }
};

int main () {
    Foo f;
    f.foo ();  // on my system returns P3Foo
    return 0;
}
0

, . :

#include <iostream>
#include <string>

#define createMyClass(x) MyClass x("x")

class MyClass{
    string _name;
    MyClass( const string& name ) : _name(name){}
    memberfunc(){
        std::cout << "Name: " << _name << std::endl;
    }
}

int main (int argc, char **argv) {
    createMyClass( ObjA );
    ObjA.memberfunc(); // prints the name
    return 0;
}
0

Source: https://habr.com/ru/post/1746539/


All Articles