Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help required regarding "this" operator in C++

akalexi312akalexi312 Member Posts: 31
Hi Friends

I am trying out a piece of code where i want to display the name of object that is about to be destroyed in destructor function.But how
do i derive the same of object using "this" operator ??

Ex : stack :: ~stack() {

cout << "I am about to destory the object" << this << endl;
delete [] array; // array was allocated using new inconstructor

}

here this is printing the address reference of object but what i want to display is the name of object.

for example stk4 is name of object which is about to be destroyed then destructor should yell it's name as stk4.

Thanks
Anand Kulkarni

Comments

  • LundinLundin Member Posts: 3,711
    : Hi Friends
    :
    : I am trying out a piece of code where i want to display the name of object that is about to be destroyed in destructor function.But how
    : do i derive the same of object using "this" operator ??
    :
    : Ex : stack :: ~stack() {
    :
    : cout << "I am about to destory the object" << this << endl;
    : delete [] array; // array was allocated using new inconstructor
    :
    : }
    :
    : here this is printing the address reference of object but what i want to display is the name of object.
    :
    : for example stk4 is name of object which is about to be destroyed then destructor should yell it's name as stk4.
    :
    : Thanks
    : Anand Kulkarni
    :


    What do you mean with "name"? If you mean the variable name, there is not way to display it, simply because there is no reason to do so. Variable names exist only in the source file, not in the program itself.
    You could however implement a look-up table where every address has a corresponding string.

    Something like:

    [code]
    class MyClass
    {
    private:
    static map<MyClass*, string> nameTable;
    ...

    public:
    MyClass(string str)
    {
    nameTable[this] = str;
    }

    ~MyClass()
    {
    cout << "Oh no, they killed " << nameTable[this] << "! Bastards!" << endl;
    }
    };


    int main()
    {
    MyClass bob("Bob");
    MyClass asdf("Joe");
    ...
    return 0;
    }

    [/code]

  • bluj91bluj91 Member Posts: 133
    [b][red]This message was edited by bluj91 at 2006-9-7 19:10:15[/red][/b][hr]
    : : Hi Friends
    : :
    : : I am trying out a piece of code where i want to display the name of object that is about to be destroyed in destructor function.But how
    : : do i derive the same of object using "this" operator ??
    : :
    : : Ex : stack :: ~stack() {
    : :
    : : cout << "I am about to destory the object" << this << endl;
    : : delete [] array; // array was allocated using new inconstructor
    : :
    : : }
    : :
    : : here this is printing the address reference of object but what i want to display is the name of object.
    : :
    : : for example stk4 is name of object which is about to be destroyed then destructor should yell it's name as stk4.
    : :
    : : Thanks
    : : Anand Kulkarni
    : :
    :
    :
    : What do you mean with "name"? If you mean the variable name, there is not way to display it, simply because there is no reason to do so. Variable names exist only in the source file, not in the program itself.
    : You could however implement a look-up table where every address has a corresponding string.
    :
    : Something like:
    :
    : [code]
    : class MyClass
    : {
    : private:
    : static map<MyClass*, string> nameTable;
    : ...
    :
    : public:
    : MyClass(string str)
    : {
    : nameTable[this] = str;
    : }
    :
    : ~MyClass()
    : {
    : cout << "Oh no, they killed " << nameTable[this] << "! Bastards!" << endl;
    : }
    : };
    :
    :
    : int main()
    : {
    : MyClass bob("Bob");
    : MyClass asdf("Joe");
    : ...
    : return 0;
    : }
    :
    : [/code]
    :
    :


    yarr,

    Varible titles or names are only existant (is that a word) in the source code of a program. when it is converted to machine code it ceases to exist. The exception of this is if you are using visual C++ and compiling to .NET framework, if you are doing this somehow you can gain access to the identifer of a class if you compiler it to debugging. But it is not common practice.

    You could also put a string object as a label in side the object you want to print it name and then make the deconstructor print it.
Sign In or Register to comment.