'this' pointer in C++ [B]

I was trying to access the object outside of its scope. For example, I tried to access 'st' from utility_fun

It threw an error 'error: 'st' was not declared in this scope; did you mean 'std'?'



The example depicted will help you to understand how to use 'this' pointer.

1. Printing 'this' in Stream ctor

2. You can't print *this (the reason will be updated)

3. Pass this as pointer in utility_fun2

4. Pass this as reference in utility_fun3

5. Convert 'this' address to a string in utility_fun4

6. Accessing members of class Stream using 'this->' in getVar

7. Accessing members of class Stream using (*this) in sampleAccessor

#include <iostream>
#include <memory>
#include <sstream> //for std::stringstream 

class Stream 
{
public:
    Stream(int var=3) : var_(var) 
    {
        std::cout << "Accessing this from Ctor: " << this << " var_ " << var_ << std::endl;
    }
    int getVar();
    void sampleAccessor();
    void sampleMutator(int var);
    
private:
    int var_;
};

auto st1 = std::make_shared<Stream>(2); 

/* All these Utility functions are trying
 * to access either st or st1 object.
 */
void utility_fun()
{
    //std::cout << "Accessing this from utility_fun() :" << st << std::endl; //Want to access the stream's address here.
    return;
}

void utility_fun1()
{
    std::cout << "Accessing global object from utility_fun1() :" << st1 << std::endl; 
    return;
}

//Passing this pointer
void utility_fun2(const Stream* str)
{
    std::cout << "Accessing this from utility_fun2() :" << str << std::endl; 
    return;
}

//Passing this to reference
void utility_fun3(const Stream& str)
{
    //std::cout << "Accessing this from utility_fun3() :" << str << std::endl; //Want to access the stream's address here.
    return;
}

//Passing this pointer and converting the address to string
void utility_fun4(const Stream* str)
{
    const void * address = static_cast<const void*>(str);
    std::stringstream ss;
    ss << address;  
    std::string name = ss.str();
    std::cout << "Accessing this from utility_fun4() :" << name << std::endl; 
}

int Stream::getVar()
{
    std::cout << "Accessing this from getVar(): " << this  << std::endl;
    //This takes Stream pointer
    utility_fun2(this);
    //To pass by reference, dereference this(Stream) pointer
    utility_fun3(*this);

    utility_fun4(this);
    // Access members using ->
    this->sampleAccessor();
    std::cout << "End of getVar(): ";
    return var_;
}

void Stream::sampleAccessor()
{
    // Access members using pointer dereferencing operators
    (*this).sampleMutator(5);
    std::cout << "Accessing member method using (*this) " << var_ << std::endl;
}

void Stream::sampleMutator(int var)
{
    std::cout << "Mutate the private member " << var << std::endl;
    var_ = var;
}

int main()
{
    auto st = std::make_shared<Stream>(1);
    std::cout << "Accessing this from Main: " << st << "\n" << st->getVar() << std::endl;

    utility_fun();
    utility_fun1();
    return 0;
}


Output:

1
2
3
4
5
6
7
8
9
10
Accessing this from Ctor: 0x161fec0 var_ 2
Accessing this from Ctor: 0x1620ef0 var_ 1
Accessing this from Main: 0x1620ef0
Accessing this from getVar(): 0x1620ef0
Accessing this from utility_fun2() :0x1620ef0
Accessing this from utility_fun4() :0x1620ef0
Mutate the private member 5
Accessing member method using (*this) 5
End of getVar(): 5
Accessing global object from utility_fun1() :0x161fec0

Click here for Godbolt link. 

References:

SO links

Comments