R00ki3 M1stak3
R00ki3 M1stak3

Reputation: 97

Using pointers to access object data members in an array

I am trying to allow user input to create new objects to add to an array. Each object has a data member that I am then trying to get and then set with different values.

As I have been reviewing this, I have been able to set the array subscript to call the constructor, get the age of the Monkey object, then to set the age to a new number, and then get the age again to "age" the monkey. I set it up as a test to make sure I am going in the right direction. But I would rather use pointer notation to access the object elements of the array because I intend to create a loop that allows the user to fill up the array full of monkey objects. Each monkey will age differently as a result of their order of creation. I am not stuck on the loop part yet ( I haven't gotten there yet). I am stuck with the pointer notation.

The faulty pointer notation is included in the code below and commented out.

Thank you!

    #include <iostream>

    class Monkey
    {
    private: 
        int age;

    public:
//Default constructor with cout so I can see what's happening.
        Monkey()
        {
        age = 10;
        std::cout << "Monkey constructed! " << std::endl;
        }
    //Destructor with cout so I can see what's happening.
        ~Monkey()
        {
            std::cout << "Destructor called. " << std::endl;
        }
    //getter function
        int getAge()
        {
            return age;
        }
    //setter function to age monkey
        void setAge()
        {
            age = age+ 1;
        }

    };
    int main()
    {
        Monkey monkeyArray[5];
        Monkey* arrayPtr = monkeyArray;


        std::cout << "Do you want to create another Monkey? " << std::endl;
        std::cout << "1.  Yes " << std::endl;
        std::cout << "2.  No " << std::endl;

        int userInput;

        std::cin >> userInput;

        int monkeyMarker = 0;

        if (userInput == 1)
        {
            //Stuff commented out because I am using the wrong syntax.

    //*(arrayPtr + monkeyMarker) = Monkey();
    //std::cout << "Monkey age is: " << *(arrayPtr +
    //monkeyMarker).getAge << std::endl;

//Using the subscript notations seems to be working fine.
            monkeyArray[0] = Monkey();
            std::cout << "Monkey age before set function called. "<< monkeyArray[0].getAge() << std::endl;
            monkeyArray[0].setAge();
            std::cout << "Monkey age after set function called to age him. " << monkeyArray[0].getAge() << std::endl;
        }




return 0;
}

Upvotes: 1

Views: 153

Answers (1)

Barmar
Barmar

Reputation: 780974

Your pointer syntax for assigning to the array element is correct:

*(arrayPtr + monkeyMarker) = Monkey();

Your syntax for accessing it is wrong because of operator precedence. . has higher precedence than *, so

*(arrayPtr + monkeyMarker).getAge

is treated as

*((arrayPtr + monkeyMarker).getAge)

which is trying to dereference the getAge function pointer.

You need to add parentheses. Also, since getAge is a function, you need to call it with ().

(*(arrayPtr + monkeyMarker)).getAge()

You can simplify this using the -> operator to indirect through a pointer:

(arrayPtr + monkeyMarker)->getAge()

Upvotes: 1

Related Questions