[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

The this pointer.

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
Ungenious
Asked:
Ungenious
  • 2
  • 2
1 Solution
 
UngeniousAuthor Commented:
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
 
SalteCommented:
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
 
UngeniousAuthor Commented:
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
 
SalteCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now