I have as a class member an array of that class (obj
) and I would like to create array of the Box
class (which is the containing class) and access xyz obj[5];
through a p
pointer object member of that Box
class.
The obj
array is private but I think I can access it using pointer. Am I correct?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class xyz{
public:
int x;
};
class Box {
public:
// Constructor definition
Box ()
{
cout<<"without constructot"<<endl;
}
xyz *p=obj;
private:
xyz obj[5];
};
int main()
{
Box b[5];;
Box *p=&b[0];
(b+2)->(obj)->x=5;
//cout <<(b+2)->(obj)->x<<endl;
}
exception thrown at (b+2)->(obj)->x=5;
this
error: expected unqualified-id before ‘(’ token
28 | (b+2)->(obj)->x=5;
| ^
main.cpp:28:10: error: ‘obj’ was not declared in this scope 28 | (b+2)->(obj)->x=5;
| ^~~