Instance's name of a given class

Discussion in 'C++' started by MN, Jan 21, 2009.

  1. MN

    MN Guest

    Hi All,

    Can any one show me how to get the instance's name of a given class in
    the next code ?

    #include <iostream>
    using namespace std;

    class test
    {
    int count;
    public:
    test ();
    ~test();
    int total(int num, test &instance_of_test);
    };

    test::test(){
    count = 0;
    };

    test::~test(){};

    int test::total(int num, test &instance_of_test)
    {
    count += num;
    cout << "Added number " << num << " from instance \"" <<
    &instance_of_test << "\" of class test\n";
    return count;
    }

    int main (){
    // Declare 2 instances of class "test"
    test tst;
    test tst1;

    int x = 0;
    int y = 0;

    for (int i = 0; i < 3; ++i)
    {
    x = tst.total(i, tst);
    }
    cout << "\nTotal is "<< x << "\n";

    cout << "\nUse another instance of class \"test\":\n" << endl;

    for (int j = 0; j < 2; ++j)
    {
    y = tst.total(j, tst1);
    }
    cout << "\nTotal is "<< y << "\n";

    return 0;
    }

    After execution I have these next lines:

    Added number 0 from instance "0x7fff3c1ce700" of class test
    Added number 1 from instance "0x7fff3c1ce700" of class test
    Added number 2 from instance "0x7fff3c1ce700" of class test

    Total is 3
    Use another instance of class "test":

    Added number 0 from instance "0x7fff3c1ce6f0" of class test
    Added number 1 from instance "0x7fff3c1ce6f0" of class test

    Total is 4

    So what to do to change "0x7fff3c1ce700" to "tst" and "0x7fff3c1ce6f0"
    to "tst1" ?
     
    MN, Jan 21, 2009
    #1
    1. Advertising

  2. MN

    Guest

    On 21 Jan., 11:47, MN <> wrote:
    > Hi All,
    >
    > Can any one show me how to get the instance's name of a given class in
    > the next code ?


    What you want to do is impossible with C++. I'd would go even further
    and state that no recent compiler will do what you want.

    Why do you want to have the names of the instances? If it is just for
    debugging purposes, then you had better get hold of a decent compiler.

    Regards,
    Stuart
     
    , Jan 21, 2009
    #2
    1. Advertising

  3. MN

    MN Guest

    I'm working with g++/gdb under fedora8.
    I thought that there is a standard mechanism, which extracts class's
    instance automatically.
    So what can I do, is to add a string in function "test::total" like in
    the next code:

    #include <iostream>
    using namespace std;


    class test
    {
    int count;
    public:
    test ();
    ~test();
    int total(int num, char *instance_of_test);
    };

    test::test(){
    count = 0;
    };

    test::~test(){};

    int test::total(int num, char *instance_of_test)
    {
    count += num;
    cout << "Added number " << num << " from instance \"" <<
    instance_of_test << "\" of class test\n";
    return count;
    }


    int main (){
    test tst;
    test tst1;

    int x = 0;
    int y = 0;

    for (int i = 0; i < 3; ++i)
    {
    x = tst.total(i, "tst");
    }
    cout << "\nTotal is "<< x << "\n";

    cout << "\nUse another instance of class \"test\":\n" << endl;

    for (int j = 0; j < 2; ++j)
    {
    y = tst.total(j, "tst1");
    }
    cout << "\nTotal is "<< y << "\n";

    return 0;
    }
    As result I get :
    Added number 0 from instance "tst" of class test
    Added number 1 from instance "tst" of class test
    Added number 2 from instance "tst" of class test

    Total is 3

    Use another instance of class "test":

    Added number 0 from instance "tst1" of class test
    Added number 1 from instance "tst1" of class test

    Total is 4
     
    MN, Jan 21, 2009
    #3
  4. MN

    Helge Kruse Guest

    MN wrote:
    > I'm working with g++/gdb under fedora8.
    > I thought that there is a standard mechanism, which extracts class's
    > instance automatically.


    No, but gdb reads debug info and can say that's this variable. No way in
    normal execution of the program.

    Helge
     
    Helge Kruse, Jan 21, 2009
    #4
  5. On 2009-01-21 11:47, MN wrote:
    > Hi All,
    >
    > Can any one show me how to get the instance's name of a given class in
    > the next code ?
    >
    > #include <iostream>


    #include <string>

    > using namespace std;
    >
    > class test
    > {
    > int count;


    const std::string name;

    > public:


    test (const std::string& name);

    > ~test();
    > int total(int num, test &instance_of_test);
    > };




    test::test(const std::string& name)
    : count(0), name(name)
    { }

    > test::~test(){};
    >
    > int test::total(int num, test &instance_of_test)
    > {
    > count += num;


    cout << "Added number " << num << " from instance \"" << name <<
    "\" of class test\n";

    > return count;
    > }
    >
    > int main (){
    > // Declare 2 instances of class "test"


    test tst("tst");
    test tst1("tst1");

    > int x = 0;
    > int y = 0;
    >
    > for (int i = 0; i < 3; ++i)
    > {
    > x = tst.total(i, tst);
    > }
    > cout << "\nTotal is "<< x << "\n";
    >
    > cout << "\nUse another instance of class \"test\":\n" << endl;
    >
    > for (int j = 0; j < 2; ++j)
    > {
    > y = tst.total(j, tst1);
    > }
    > cout << "\nTotal is "<< y << "\n";
    >
    > return 0;
    > }


    I have neither tested, nor compiled the code, but I think you get the idea.

    --
    Erik Wikström
     
    Erik Wikström, Jan 21, 2009
    #5
  6. MN wrote:
    > class test
    > {
    >         int count;
    > public:
    >         test ();
    >         ~test();
    >         int total(int num, char *instance_of_test);


    The instance_of_test should be of type const char * to be used in the
    lines bellow

    >                 x = tst.total(i, "tst");

    <snip>
    >                 y = tst.total(j, "tst1");


    --
    Marcelo Pinto
     
    Marcelo Pinto, Jan 21, 2009
    #6
  7. MN

    James Kanze Guest

    On Jan 21, 3:20 pm, Victor Bazarov <> wrote:
    > MN wrote:
    > > I'm working with g++/gdb under fedora8.
    > > I thought that there is a standard mechanism, which extracts class's
    > > instance automatically.


    > The instance is pointed to by the 'this' pointer. That's the
    > mechanism.


    > As far as the object is concerned, it has no name. It's your
    > imagination that there is "tst" or "tst1" or whatever. It
    > only exists while the program is in the source form. After
    > it's compiled, there is no single way to get back to the
    > source form. You can rewrite your program by changing the
    > names of your objects and classes to any of the allowed
    > combinations of symbols and get exactly the same final
    > compiled program. There are few limitations on that, like
    > 'main' has to be 'main' and cannot be 'start', or that
    > 'std::cout' has to be 'std::cout' in order to be picked up
    > from the library.


    Just to expand on this: what would the name of the intance be if
    I used a temporary "text()"? Or for an object allocated by new?
    As Victor said, names are purely compile time. Some objects
    (most, in a lot of real applications) don't have any name;
    others may have more than one name (depending on what you
    consider a "name").

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 22, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lord0
    Replies:
    1
    Views:
    599
    Thomas Weidenfeller
    Apr 19, 2006
  2. ak
    Replies:
    2
    Views:
    346
  3. chiara
    Replies:
    6
    Views:
    501
    Barry Schwarz
    Oct 6, 2005
  4. Ami
    Replies:
    3
    Views:
    440
    Tomás
    Feb 27, 2006
  5. 2Barter.net
    Replies:
    0
    Views:
    396
    2Barter.net
    Dec 13, 2006
Loading...

Share This Page