Hello people,

I am new to this community and the only reason for why I joined here is to learn and get all my doubts clarified as I am learning C++ all by myself and also I dont have any computer literate people around me in my place.

I started with the some online and it has been around 2 weeks since I started with C++. I feel as if i understood when read the theory part but when it comes to coding I dont really know where or what am i should be strating with :sad: I just tried writing a small snippet to understand THIS Pointer. First of all, is it called a This pointer or a This operator? I have seen some articles using the term pointer and operator interchangeably to address 'this'.

Here is my snippet: (Please don't laugh at it as I am learning it, It might even be a bluder :-| )

#include <iostream.h>
class A
{
int x;
fn() { return this->x; }
};
void main()
{
A a;
int x=10;
a.fn();
}

It gives me a compilation error stating A::fn()' is not accessible in function main() But why? I did not declare the fn() function as a private. So, should my main function not be having access to the fn() of class A?

I am also recieving a warning like 'x' is assigned a value that is never used Why is this?

Now what should I be doing to make this work? By the way, I am trying to find out the address of the memory which is held by the THIS pointer. I might sound so silly but please bear with me. :rolleyes:

>>t of all, is it called a This pointer or a This operator?

this is a pointer. An operator is something else.

c;ass pbkects are private by default. If you want to make them public then you need to specify it.

functions need to have a return type, c++ does not permit default return types.

class A
{
private:
    int x;
public:
    int fn() { return this->x; }
};

>> am also recieving a warning like 'x' is assigned a value that is never used
Because you have to initialize variables to something (usually 0) before they can be used. If not initialized they will just contain some random value. Class variables are normally initialized in the class constructor, like this:

class A
{
private:
    int x;
public:
    A() {x = 0;}
    int fn() { return this->x; }
};

>> I am trying to find out the address of the memory which is held by the THIS pointer
The actual address is not important to most programmers, but it is the address of the class's instance. In the main() function you posted it would be the address of object a.

Member Avatar for GreenDay2001

Hi, I am too a C++ student(in school) from India only.:)

am i should be strating with :sad: I just tried writing a small snippet to understand THIS Pointer. First of all, is it called a This pointer or a This operator? I have seen some articles using the term pointer and operator interchangeably to address 'this'.

Well it is this pointer . This operator is "*" which when precededby pointer name refers to its value, and when not it the the address stored in pointer,i.e. actual value. Please don't laugh at it as I am learning it, It might even be a bluder. Dont worry about that....everyone was once newbie.

#include <iostream.h>
class A
{
int x;
fn() { return this->x; }
};
void main()
{
A a;
int x=10;
a.fn();
}

The problem with this is that you have declared both data and function as private, even when you havnt used private keyword. Thats because a class treats everything private until l encountered with a public keyword. So, using private is optional, but increases a little clarity in code.

The correct code must be something like this.

#include <iostream.h>

class A
{
// private:            
    // you see its optional
    int x;                
public:
    int fn() {x=10; return this->x; }  // there should be return type of function
};
void main()
{
    A a;
    //int x=10;            you cann't change private value outside class
    cout << a.fn();    // its just for viwing output
}

Yes, it would work with all old compilers, but when you work on some new compilers

#include <iostream>

class A
{
// private:            
    // you see its optional
    int x;                
public:
    int fn() {x=10; return this->x; }  // there should be return type of function
};
int main()
{
    A a;
    //int x=10;            you cann't change private value outside class
    std::cout << a.fn();
    std::cin.get();
    return 0;
}

Now what should I be doing to make this work? By the way, I am trying to find out the address of the memory which is held by the THIS pointer. I might sound so silly but please bear with me. :rolleyes:

Is this what you want

#include <iostream.h>

class A
{
// private:            
private:
    int x;                
public:
    int fn() {x=10; return this->x; }
    int* pnt() { return &x; }  // returns a integer type pointer
};
void main()
{
    A a;
    cout << a.pnt();
    cin.get();
}

Hey Ancient dragon and Vishesh,

Many thanks for your replies. You guys are so friendly unlike the other forum (i dont want to mention the name) where I have to feel embarrased :mad: Well, not everyone are the same yeah? :)

I should tell you, I have read these key point, "All the members and methods in any class are private by default". But damn this basic concept has slipped off my mind when I coded. It's so bad I did not even realise it by seeing the error. I should now learn to correlate the basic theoritical concepts while coding. Also, thanks for the note that 'All class members should be initialized basically in the class constructor'. I did not know this before. Now my code is getting compiled successfully :)

But the below code which Vishesh have quoted will return the address where the variable x is stored if I am not wrong. I want the memory location of the object. I mean when ever i say A a; a memory will be allocated for the object a in the heap right? I want to find that memory location using 'this' pointer. When I say this->x it returns the value of x and when I say int *ptr () {return &x;} it will return the address where the variable x is stored. Now how do I find the address where in the memory my object is created?

>> Now how do I find the address where in the memory my object is created?
The address stored in the this pointer is the address of the object. Here is an example that illustrates this. Notice that the value displayed in lines 8 and 18 are the same address.

#include <iostream>

using namespace std;
class A
{
public:
	A() {x = 0;}
	int getX() {return this->x;}
	void printThis()
	{
		cout << this << endl;
	}
protected:
	int x;
};

int main()
{
	A a;
	a.printThis();
	cout << "&a = " << &a << endl;
	cin.ignore();

	return 0;
}

>>But damn this basic concept has slipped off my mind when I coded
Don't worry, it will come in time. Practice makes Perfect is one of the golden rules. We were all beginners at some time.

wow! yes this is what i wanted to understand. I have remodified my code to understand myself in a much better way.

#include <iostream.h>
#include <conio.h>
class A
{
public:
void printThis()
{
cout << "Now tha value of this is " << this << endl;
}
};

void main()
{
clrscr();
A a;
cout << "&a = " << &a << endl;
a.printThis();
A b;
cout << "&b = " << &b << endl;
b.printThis();
getch();
}

This means the "THIS pointer will have the address of the recently created object (or the current object) at any given point of time". I just can't stop smiling :) It took me a day to understand a single statement practically. Thanks for the guidence! By the way, a small query... when I used cout << this; directly in the main function next to the line cout << &a; the compiler threw an error stating 'this' can only be used with in a member function and that is why I used that printThis() in the class A as you have used above in your example. But why is that we must use THIS only inside a member function?

>THIS pointer will have the address of the recently created object
No, this pointer only has the address of the current object.

>But why is that we must use THIS only inside a member function?
Because otherwise it (compiler I guess) would have no way of knowing which object you're referring to. Using the this pointer allows you to quickly get an address of the current object without having to pass it in as a function argument.

Hope that made sense.

Yup, It did! Many thanks Joe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.