Link to home
Start Free TrialLog in
Avatar of Ungenious
Ungenious

asked on

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
Avatar of Ungenious
Ungenious

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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