Returning the value of a class member pointer variable using 'this' pointer

Vishnu CS

My scenario is , I want to return the value of class member variable m_ptr using this pointer.

What I have tried is ,

#include <iostream>

using namespace std;

class Test{
    int *m_ptr;
    
    
    public:
        Test():m_ptr(nullptr){
            cout<<"Def constr"<<endl;
        }
        
        Test(int *a):m_ptr(a){
            cout<<"Para constr"<<endl;
        }
        
        ~Test(){
            cout<<"Destr "<<endl;
            delete m_ptr;
        }
        
        int getValue(){
            return *m_ptr;
        }

};

int main(){
    
    Test obj(new int(34));
    cout<<"Value -"<<obj.getValue()<<endl;
    return 0;
}

The console output is

Para constr
Value -34
Destr

This is fine.

What I am trying to do now is ,

I want to modify the getValue function to return the value of pointer variable m_ptr using this pointer like below . (only writing the getValue function)

int getValue(){
    return this->(*m_ptr);
} 

But this throws the error as,

[Error] expected unqualified-id before '(' token

I am beginner in this c++ and I am not understanding the actual reason for this error. It will be helpful to explain what I am doing wrong here.

eerorika

The indirection operator is in the wrong place. this->m_ptr would be correct to access the member, and to indirect through that pointer, you put the indirection operator on the left side:

return *this->m_ptr;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why returning a member variable using "this" pointer is not allowed, but setting a member variable using "this" is allowed?

Can I use "using" instead of "typedef" for a pointer to class member variable?

Returning pointer on class member from const member function

Unable to assign a class member pointer variable

Why defining buffer length of a buffer leads to that class's function member to loose the value of a function pointer member variable?

How to change class member value by pointer

Proper 'using' syntax for pointer-to-member variable

Is it legal to cast a pointer-to-derived-class-member-variable to a pointer-to-base-class-member-variable?

Assign value to member of struct using double pointer

function pointer member variable

Pointer to class data member "::*"

Should the member of class be a pointer?

Pointer to class member function

Pointer to managed class member

pointer to a member function of a class

returning a class object with a pointer

Pointer to member variable as static member

Storing member function pointer from arbitrary class as class instance variable

A class with a pointer pointing to another class as a member variable and pushing it into vector

Differences between using pointer to the class member and the structure member

Typecasting Member Variable Pointer to Object Pointer

How do I dereference the return value of a function returning a pointer without using a temporary variable?

Using pointer to member function of a base class on object of derived class

C# declare class member variable type of itself pointer

Using char pointer to print value of an integer variable

Changing the variable's value, by using a pointer

Query regarding pointer member variable

Initializing member variable with pointer to itself

setting a pointer member variable correctly