Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. how to get object name

how to get object name

Scheduled Pinned Locked Moved Managed C++/CLI
tutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K Sant
    wrote on last edited by
    #1

    hi , i am trying to get the object name, see the code below, but not able to find method, what could be the code in place of ???????. #include "iostream" using namespace std; class A{ public: int i; void printObjectname(){ cout << ????????;//<--what could be here, to print the object name as 'objectA' } }; int _tmain(int argc, _TCHAR* argv[]) { A *objectA = new A(); objectA->printObjectname(); return 0; } santosh

    L 1 Reply Last reply
    0
    • K K Sant

      hi , i am trying to get the object name, see the code below, but not able to find method, what could be the code in place of ???????. #include "iostream" using namespace std; class A{ public: int i; void printObjectname(){ cout << ????????;//<--what could be here, to print the object name as 'objectA' } }; int _tmain(int argc, _TCHAR* argv[]) { A *objectA = new A(); objectA->printObjectname(); return 0; } santosh

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this can not be done. new A() is an object, it could have a name. objectA is not an object, it is a variable, it holds a reference to the class A object. There could be many more references to the same object, it suffices to do A* objectB=objectA;, now how would the class A object have to know the name of all the variables pointing to it, and in particular the one you want?? Suggestion: give your objects a name by providing a Name property and/or a name argument in their constructor, so new A("aha"); would create a new class A object and give it the name "aha". Now you can ask that object for its name, and/or you can override its ToString() method so it identifies itself. Remark: this works independently of the variable(s) that may refer to that object! :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups