Search code examples
c++vectorbooleanoperators

how does the structure dereference operator work?


I am a Java programmer trying to teach myself C++.

I would like to understand how the structure dereference operator works. Specifically, what does the following line of code do in explicit terms?

    if (elements[i]->test(arga, argb)) {}

test(arga,argb) is a Boolean function in the same class, and elements is a vector of instances of the element class. Here is the code that immediately surrounds the line above, about which I am interested:

for (unsigned i = 0; i < elements.size(); ++i) {
    T arga = INFINITY, argb = INFINITY;
    //using namespace std;
    //std::cout >> elements[i] >> std::endl;
    //std::cout >> test(arga, argb) >> std::endl;
    if (elements[i]->test(arga, argb)) {
        //some code
    }
}

It seems that the if line is testing to see whether or not the boolean returned by test(arga,argb) is part of the given instance of the elements class. But when I try to expose the underlying values of elements[i] or test(arga,argb) with the cout lines above, the compiler throws errors until I comment those lines out.

In Java, I would be able to fiddle around with this until I found values of each that correspond with each other, and then I would understand the line of code. How can I figure out what this line of code does in C++? I would be interested to see some references online if they are available.


Solution

  • Since numerous respondents told me that I need to provide the code before they can answer, I looked deeper in the code, and re-wrote something which tells me that the line:

    if (elements[i]->test(arga, argb)) {}  
    

    is a test to see whether or not the boolean member function of elements[i] is true.

    The c++ program that I wrote to identify the meaning of -> in this context is:

    #include "stdafx.h"
    #include <vector>
    #include <string>
    #include <iostream>
    
    template<typename T>
    class Bone
    {
    public:
        std::string myName;
        int mySize;
        Bone(const std::string &name, const int &size) : myName(name), mySize(size)
        {}
        bool isBigger(const int &testSize) const
        {
            if (testSize > mySize) return false;
            else return true;
        }
    };
    
    int main(int argc, char **argv)
    {
        std::vector<Bone<float> *> bones;
        // name, location, size
        bones.push_back(new Bone<float>("femur", 10));
        bones.push_back(new Bone<float>("ulna", 4));
        bones.push_back(new Bone<float>("maxilla", 3));
    
        int testSize = 6;
        // test each bone to see if it is bigger than testSize
        for (unsigned i = 0; i < bones.size(); ++i) {
            if (bones[i]->isBigger(testSize)) {
                std::cout << bones[i]->myName; std::cout << " is bigger than testSize! " << std::endl;
            }
        }
    
        while (!bones.empty()) {
            Bone<float> *thisBone = bones.back();
            bones.pop_back();
            delete thisBone;
        }
        return 0;
    }
    

    Thank you to everyone who led me to figure this out.