C++ this pointer - Stack Overflow

I am trying to learn the basics of C++ from an elementary book.I am trying to solve this question...&

I am trying to learn the basics of C++ from an elementary book. I am trying to solve this question... "If there are four objects in a program, how many "this" pointers would exist for these objects and why?"

My initial instinct was that there is only one "this" pointer and it points to the object whose member function calls the "this" pointer. But there was no basis for my answer and so I tried to verify it. I thought if there are multiple this pointers in a program, they would all be saved at different locations in memory. If there is only one, the address of the "this" pointer would always remain the same. So I wrote the following program and ran into an lvalue error.

#include <iostream>
#include <cstring>
using namespace std;

class person
{
    char name [10];
    int age;
    public:
    
    person () {}
    
    person (const char *s, int a)
    {
        strcpy (name, s);
        age = a;
    }
    
    void display (void)
    {
       cout << "Address of this pointer is " << &this << endl;
    }
};

int main()
{
    person A ("Jimmy", 12), B ("Joe", 44);
    A. display ();
    B. display ();
    return 0;
}

Can you please suggest corrections to the above program or any other way to do it?

I am trying to learn the basics of C++ from an elementary book. I am trying to solve this question... "If there are four objects in a program, how many "this" pointers would exist for these objects and why?"

My initial instinct was that there is only one "this" pointer and it points to the object whose member function calls the "this" pointer. But there was no basis for my answer and so I tried to verify it. I thought if there are multiple this pointers in a program, they would all be saved at different locations in memory. If there is only one, the address of the "this" pointer would always remain the same. So I wrote the following program and ran into an lvalue error.

#include <iostream>
#include <cstring>
using namespace std;

class person
{
    char name [10];
    int age;
    public:
    
    person () {}
    
    person (const char *s, int a)
    {
        strcpy (name, s);
        age = a;
    }
    
    void display (void)
    {
       cout << "Address of this pointer is " << &this << endl;
    }
};

int main()
{
    person A ("Jimmy", 12), B ("Joe", 44);
    A. display ();
    B. display ();
    return 0;
}

Can you please suggest corrections to the above program or any other way to do it?

Share Improve this question edited Mar 4 at 7:18 Vinayak Deshmukh asked Mar 3 at 8:07 Vinayak DeshmukhVinayak Deshmukh 3811 silver badge7 bronze badges 19
  • 9 Whether any this pointer exists at all is a philosphical question. &this makes as much sense as &1. – molbdnilo Commented Mar 3 at 8:10
  • 4 Naively summarized, "each instance of an object has its own this pointer". This pointer points to the unique state (e.g. a struct containing all data of a class) and is implicitly passed on to all (non-static) member function calls. So you can think of void display() becoming something like a stand-alone function void display(person* this). – Pepijn Kramer Commented Mar 3 at 8:21
  • 2 Side note, I still see a lot of "C" style coding in your example. void display(void) the void argument is not needed. And for strings learn to use std::string – Pepijn Kramer Commented Mar 3 at 8:22
  • 2 That &this does not work points out this is not actually a pointer, but syntax for one you never can name in advance. If you create a variable O storing an instance of class C that has non static members m and n, where m is a method of the class then within method m this.n is &O.n. But as you can instanciate many objects from the same class you never could name any specific pointers, so this is part of the C++ language for that matter. With any number of objects the same number of this pointers exist, I agree with Ted. The book could also expect the answer none, though. – LegacyDev Commented Mar 3 at 8:36
  • 8 This is not a correct question, this is not an attribute of an object. The notion of counting this makes no sense. Besides, this question is shallow. With multiple inheritance, a single object, in a sense, has multiple this. Unfortunately, you cannot trust all books. I am familiar with some books containing total trash. – Sergey A Kryukov Commented Mar 3 at 8:48
 |  Show 14 more comments

1 Answer 1

Reset to default 8

There are a few things that we know about this : it exists inside member functions of classes (except static members), and it is used to refer to a specific instance of that class (i.e. an object). It's an expression without an address. (Common enough, the number 3 is also an expression without an address).

What we don't know about this is what happens to it outside a member function. Does the compiler bother to store it? Often, there's no need to. If you have std::array<person, 500> A, then the compiler can easily calculate the this pointer that's needed to call A[42].display(). The compiler will derive it from the this pointer of A itself. But at other times, the compiler can store it.

And if you don't actually use this in a member function (not even implicitly, to access members), the optimizer may decide to remove the unnecessary calculation.

Hence, the answer depends on the exact compiler, compiler settings and the exact point in your program.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104520a4611478.html

相关推荐

  • C++ this pointer - Stack Overflow

    I am trying to learn the basics of C++ from an elementary book.I am trying to solve this question...&

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信