this

Why is there a problem i am trying to find adress of w2 by passing it through an object of w1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  class nothing
{
private:
	int x;
public:
	void reveal(nothing w)
	{
		cout<<"THE object adress is : "<<this<<endl;;
	}
};
int main()
{
	nothing w1, w2 ,w3;
	w1.reveal(w2);
	getch();
}
initialise (and pass by reference).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>


class nothing
{
private:
	int x;
public:
	nothing(int p) : x(p){}

	void reveal(nothing& w)
	{
		std::cout<<"THE object address is : " << this <<std::endl;;
	}
};

int main()
{
	nothing w1(0), w2(0) ,w3(0);
	w1.reveal(w2);
}
Last edited on
1
2
3
4
5
6
 void reveal(nothing w) //the object you pass will be copied here and thus being at a different address
	{
		cout<<"THE object adress is : "<<this<<endl; //and here you print the address of 
//(the way you call it in main) w1 not the passed copy of w2
	}
};
Last edited on
mutexe your method works but why doesn't it work when i pass it by value p.s could you do it without giving it parametrized constructor , default constructor would clear my another problem
Glandy why should it be the address of w1 i am passing w2 and doesn't this give the address of passed value or does it give the address of the thing that calls it ?
I find pointer particulary hard cause it's tricky to know which address it is giving
why is the function requiring parameter but not using it ?

am trying to find adress of w2 by passing it through an object of w1


i'm not sure why you want that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

class nothing
{
private:
    int x;
public:
    nothing* getThisPointer() { return this; }
    void reveal(nothing& w)
    {
	cout<<"THE object adress is : " << w.getThisPointer() <<endl;
    }
};

int main()
{
    nothing w1, w2 ,w3;
    w1.reveal(w2);
}

Last edited on
why is the function requiring parameter but not using it ?

Good point :) i see now why mutexe did it by reference and it can not be done by value and your program does work though in line 9 you did another thing which i didn't know it was possible you made an instance of pointer to a function i didn't know you could do that too
Topic archived. No new replies allowed.