Reputation:
If I have the following basic C++ program:
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);}
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
int main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area() <<endl;
cout <<&rect<<endl;
cin.get();
return 0;
}
is the last print statement printing the address of the variable rect or the address of the object? are they the same? or are they the same?
Upvotes: 0
Views: 308
Reputation: 17014
rect
is just an identifier for an object in the stack. In this case, an instance of CRectangle
.
By calling &rect
, you'll be getting the address, in the stack, where the object resides.
Upvotes: 0
Reputation: 1179
There is no such thing as an address of a Class if thats what you mean? &CRectangle does not exist , only an address of an instance of the class (&rect) exists. No memory is occupied by the Class definition itself.
Upvotes: 3
Reputation: 28762
The variable rect
is an object of CRectablge
, so there is no difference between the address of the variable and the object in this case.
Upvotes: 0
Reputation: 7010
They are the same. It's printing the address of rect which is the same as the address of the object. Rect is on the stack, and thus the entire object is as well.
Upvotes: 4