Assign the public object member address to a pointer : Public « Class « C++






Assign the public object member address to a pointer

Assign the public object member address to a pointer
  

#include <iostream>
using namespace std;

class MyClass {

public:
  int i;
  MyClass(int j) { 
     i = j; 
  }
};
int main()
{
  MyClass myObject(1);
  int *p;
  p = &myObject.i;                     // get address of myObject.i
  cout << *p;                          // access myObject.i via p
  return 0;
}


           
         
    
  








Related examples in the same category

1.Illustrates the use of a public variableIllustrates the use of a public variable
2.public class extending
3.field in struct is public by default
4.Class with only public fields and methods
5.Virtual public inheritance
6.public data field
7.public inheritance from three parent classes