-4

I want to access and print the class pointer member variable from main() without using any class member functions.

#include <iostream>
#include <string>
using namespace std;

class MyVector {
    int* mem;
    int size;
public:
    MyVector();
    MyVector(int n, int val);
    ~MyVector() { delete[]mem; }

    // **I don't want to use this show() function**
    void show(){
        for (int i = 0; i < 10; i++)
        {
            cout << mem[i] << endl;
        }
    }
};

MyVector::MyVector() {
    mem = new int[100];
    size = 100;
    for (int i = 0; i < size; i++)
    {
        mem[i] = 0;
    }
}

MyVector::MyVector(int n, int val)
{
    size = n;
    for (int i = 0; i < size; i++)
    {
        mem[i] = val;
    }
}

How do I modify the code to access it like mem[index] in the main() function?

int main()
{
    MyVector mv;
    mv.show();
}
  1. The pointer mem variable of the class should remain the same.
  2. I want to print the mem variable without using the show() function.
  3. I want to know how to modify the code to access the mem[index] form.

For example:

int main()
{
    MyVector mv;
    for (int i = 0; i < 5; i++)
    {
        cout << mem[index];
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Does this answer your question? [overload array operator for mystring class](https://stackoverflow.com/questions/33967700/overload-array-operator-for-mystring-class) – Stephen Newell Apr 19 '22 at 17:06
  • If you can get access to the private members, you can copy the pointer to a local variable called `mem`. If you've got a typo in the last code snippet though want to use `mv[i]` instead of `mem[index]` though: this requires a implementation of the `[]` operator as member operator; you cannot implement the `[]` operator at namespace scope. – fabian Apr 19 '22 at 17:08
  • "*I want to print the mem variable without using the show() function*" - why? – Remy Lebeau Apr 19 '22 at 17:09
  • Overloading the `operator[]` in the class will violate the OP's "*without using any class member functions*" requirement. – Remy Lebeau Apr 19 '22 at 17:10
  • 1
    @remy Would be nice to know why this is a requirement at all, otherwise I tend to believe that `MyVector` is some legacy stuff, which cannot be changed at all, or looking at some xy-problem. The OP needs to clarify that, before we can give an answer solving the real problem. – πάντα ῥεῖ Apr 19 '22 at 17:30
  • Does this answer your question? [Can I access private members from outside the class without using friends?](https://stackoverflow.com/questions/424104/can-i-access-private-members-from-outside-the-class-without-using-friends) – BoP Apr 19 '22 at 18:36

2 Answers2

1

C++ How can print dynamically allocated class variables without using class member functions

You can name data members outside of member functions if the members have public access (or if accessed from a friend). The data members in your example have private access, so they cannot be named outside of member functions (or friends).

That said, public data members cannot be looked up directly in a scope even if there is an instance of the class, so you cannot use mem in main to refer to the member. To access a non-static data member, you must use the member access operator . to access the member through the instance.

You could achieve "I want to know how to modify the code to access the mem[index] form." by making a copy of the member:

class MyVector {
public:
    int* mem;
// ...

MyVector mv;
int* mem = mv.mem;
std::cout << mem[index];

That said, given that you delete the member in destructor, it is an owning pointer, and it would be a terribly bad idea to make such pointer public. The encapsulation is essential to prevent code from outside of the class from breaking class invariants that are necessary to avoid memory leaks and undefined behaviour.

Also, your class is badly broken. If you make a copy (possibly without intending to do so), the behaviour of the program will be undefined due to deleting the same pointer more than once.

Also, the constructor MyVector::MyVector(int n, int val) fails to initialise mem and indirects through it, so the behaviour of the program will be undefined if you use that constructor.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

You would have to make the class members be public:

class MyVector {
public:
    int* mem;
    int size;
...
};

And then access them via your mv variable:

int main()
{
    MyVector mv;
    for (int i = 0; i < 5 /* or: mv.size */; i++)
    {
        cout << mv.mem[i];
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770