?
Solved

The this pointer.

Posted on 2003-03-19
4
Medium Priority
?
297 Views
Last Modified: 2011-09-20
What would the correct syntax be for using the this pointer with an array?

e.g.)  

...
cout << this->array[1];
...

doesn't work.  Neither does ... cout << (this->array)[1];

Thanks,

-Ungenious
0
Comment
Question by:Ungenious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:Ungenious
ID: 8170085
This may be the same question, but how would the syntax on a delete operation look like on an array?

e.g.)

...
delete [] array;
...

Where would I place the this-> pointer if I wanted it to point to array?

(Although it may or may not be the same answer, points have been increased due to the addition of this question.)

-Ungenious
0
 
LVL 12

Accepted Solution

by:
Salte earned 100 total points
ID: 8170273
Not quite sure what you mean.

An array doesn't have a this pointer.

Arrays are fundamental data type in C++ but is not a class or struct.

this pointer is something that is defined for non-static member functions of classes and structs.

if you define an array as member in a class and also a non-static member function you can write:

class X {
private:
   int arr[5];
public:
    void func()
    {
       this -> arr[3] = 2;
    }
};

If you want, the this -> can also be omitted in which case you simply write:

arr[3] = 2;

instead.

However, for a plain array and used in code outside of non-static member functions, the 'this pointer' isn't defined nor would it have any meaning to speak of any 'this pointer' in such a context and so using 'this' either with arrays or without is not meaningful.

If the array is an array of class objects, then you still have no this pointer until you call a non-static member function on a particular element of the array and in that case the this pointer will point to that element:

class Y {
private:
   int k;
public:
   void foo(int z) { k = z; }
};

Y arr[3];

arr[2].foo();

In this case inside the body of foo() the this pointer will point to arr[2] so the value is &arr[2] which is the same as arr + 2.

However, the function foo will not know it is called for an array member.

Y v;

v.foo(); will be exactly the same function but with the this pointer pointing to the object v, so this will have the value of & v this time.

In another example you have:

Y * p = & arr[1];

p -> foo();

This time the this pointer will have the value of the pointer p which also happen to be pointing into the array.

Note that static functions have no this pointer and so cannot refer to members of any particular object. They can only refer to static members which are shared among all objects of that class.

Since the this pointer only point to a specific class instance it will never point to an array - only to a single element of an array or a single object not part of an array. The this pointer can even point to a member of an outer class:

class Z {
private:
   Y v;
public:
   void bar(int n) { v.foo(n+2); }
};

Z q;

q.bar(7);

Here the call to q.bar(7) will cause the function Z::bar to be called with a this pointer pointing to q, i.e. this will equal & q. Inside bar we call Y::foo with argument 7+2 == 9 and the this pointer will have a value of:

& this -> v == & q.v, so Y::foo's this pointer will point to q's element named v.

Hope this is of help.

Not quite sure if I understand your second question either, but yes, you delete an array allocated on heap by:

delete [] arr;

provided the array was allocated by something like:

arr = new some_type[some_value];

However, since this pointer always point to single elements and never to an array, it won't make sense to do anything like:

delete [] this;

Indeed, doing delete this is - although possible - seldom very wise to do - unless you know what you're doing. For one thing, any attempt to access elements after is a bad idea.

Alf
0
 

Author Comment

by:Ungenious
ID: 8170337
Alright:

First, I didn't have enough explanation in my original question/s.

Secondly, I had an entirely different problem that I didn't realize.  And the format: this->array[x]  DOES work.


My original question should have been stated something like:

class Z{
     private:
          int array[3];
     public:
          someFunction(int *);
};
//and I have a member fxn
Z::somefunction(int *array){
      etc. etc. etc.
}

in this case I would want to be able to use the 'this' pointer so I could differentiate between the two arrays.  

I was getting some error b/c of a stupid mistake I made in initializing my dynamically allocated int array, and I thought it was partially b/c of the 'this' pointer.

Thanks Salte for all the info though. =)
Sorry for the bad question.
-Ungenious
0
 
LVL 12

Expert Comment

by:Salte
ID: 8172798
Yes, if you have a class member and a local argument or parameter to the method with the same name you can use the this pointer to differentiate between the two:

class X {
private:
   int x;

public:
   void foo(int x);
};

void X::foo(int x)
{
   int k = x; // read argument x.
   x = 7; // set argument x.
   int v = this -> x; // read class member x.
   this -> x = x; // set class member x to value of argument x.
}


However, this has nothing to do with arrays per se. Of course, x could have been an array but that is kinda irrelevant to the issue.

So, yes, this -> array[x] does work but the fact that it is an array has little to do with it, it's just that:

this -> foo;

refer to a class member named foo

foo

will by default first look for a local variable/argument named foo and if there is none will look into class members and see if there's a class member named foo.

This is C++'s scoping rules and has nothing to do with what type "foo" above is or isn't.

These scoping rules are kinda hairy and non-intuitive in some ways, partly due to legacy reasons:

struct foo {
   int bar;
};

struct bar {
   double foo;
};

struct foo bar;
struct bar foo;

now yoiu can say: foo.foo = bar.bar;

just saying "foo" will depend on the context. If the syntax call for a typename it will mean 'struct foo' while otherwise it will refer to the variable named foo which is of type 'struct bar'.

If you want the type anyway you can say "struct foo" which will always refer to the type.

so sizeof(foo) == sizeof(struct bar) == sizeof(double)
sizeof(bar) == sizeof(struct foo) == sizeof(int).

You can make very confusing code if you start defining variables and structs in such manner.

Alf
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question