1

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;
      |          ^~~
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
user786
  • 3,902
  • 4
  • 40
  • 72
  • Depends on what you're asking. You can't access `obj` directly through a pointer to a `Box` (as you see on the error). You can access and manipulate `xyz* p` though, which points to `obj[0]`. `(b+2)->p->x` gives what you want, but why make `obj` private if you're going to expose it nevertheless? – Lala5th Aug 11 '21 at 11:41
  • Since `obj` is a `private` member of `Box`, `(b + 2)->(obj)` will fail (since the member `obj` is inaccessible in `main()`). However, the member `p` of `Box` is initialised, for each instance of `Box`, to point at the first element of the array `obj`. So `(b + 2)->p->x` will work. That said, it is bad practice to have a `public` member of a class (the member `p` of `Box`) that provides unfettered access to a `private` member of the same class (the member `obj`). The point of making a member `private` is to limit what code can access it. – Peter Aug 11 '21 at 11:52
  • @Lala5th Is this correct ( (b + 2)->(p+1) )->x = 5;? – user786 Aug 11 '21 at 12:02
  • @Peter Is this correct ( (b + 2)->(p+1) )->x = 5;? – user786 Aug 11 '21 at 12:03
  • 1
    @user786 `((b + 1)->p + 1)->x = 5` would be the correct way to address this, but you should use something like: `b[1].p[1].x = 5`. It greatly improves readability – Lala5th Aug 11 '21 at 12:08
  • @Lala5th I did this way so I can learn more about pointers because I imagine this is how stl is implemented with lots of use of pointers in iterator and this and that. Correct me if I am wrong – user786 Aug 11 '21 at 12:16
  • It is true that implementing the C++ standard library (and iterators for standard containers) would require understanding of pointers. I wouldn't bet they would use pointer syntax like you are though - your syntax is, as you are demonstrating, hard to read and also hard for mere mortals to get working right, which isn't a good thing in libraries that lots of people (e.g. customers of compiler vendors) will use and expect will "work out of the box". Also, BTW, your terminology is way out of date - what was the called STL (over 20 years ago) is now a part of the C++ standard library. – Peter Aug 11 '21 at 12:49

1 Answers1

2

There are a number of issues in your code. First, I'm not sure what you are trying to accomplish using the parentheses around obj but the member access operator (->) doesn't work like that – it must be followed immediately by the member's name.

Maybe what you want is something like this: ( (b + 2)->obj )->x = 5; ? However, the outer parentheses are not necessary in such an expression, because -> has left-to-right associativity (although you can use it for clarity).

However, even with that modification, you are still trying to access a private member of the class from outside the definition of that class, and that is not allowed. Generally speaking, if a member is private, then you can only access it from within member functions (or friend functions) of the class.

As a final observation: I'm not sure why you are using (b + 2) when you can just use b[2]. Did you mean to use the pointer (p) that was assigned the address of b[0] in the previous line (as in (p + 2)->obj->x = 5;)?


As a footnote, I would strongly suggest that you read the following Stack Overflow posts:

  1. Why should I not #include <bits/stdc++.h>?
  2. Why is "using namespace std;" considered bad practice?
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • @user786 No. Your original idea (using `b + 2`) does work, it's just that I don't understand why you're using that, especially as you have declared a pointer (`p`) that holds the address of the array. Anything following the `->` must be **just the name of a member**. – Adrian Mole Aug 11 '21 at 11:59
  • Is this correct `( (b + 2)->(p+1) )->x = 5;`? – user786 Aug 11 '21 at 12:00
  • Can u please tell – user786 Aug 11 '21 at 12:01
  • No. Any special punctuation or other character after the `->` will be an error. ***Only the name of a member of the class pointed to is allowed.*** – Adrian Mole Aug 11 '21 at 12:03
  • Then how to access p+1? Which will be obj[1]` – user786 Aug 11 '21 at 12:08
  • 1
    Assuming all other issues are fixed, then `(b + 2)->p` will point to the `obj[0]` member, so we can increment the result of that *entire expression*: Either `( (b + 2)->p )[1]` or `( (b + 2)->p ) + 1` should do the trick. – Adrian Mole Aug 11 '21 at 12:14
  • Is the use of pointers is common is real projects? In C++ gnu versions – user786 Aug 11 '21 at 12:18