Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Need help understanding classes, objects and shared_ptr in C++?

+2 votes
asked Oct 20, 2021 by Areeb Sherjil (1,960 points)
closed Oct 25, 2021 by Admin
Hi,

Please can anyone advise me on the following code which is based on a linked list?

This is a sample code that computes the largest object in the class Truckload:

#include "RandomBoxes.h"
#include "Truckload.h"
#include<iostream>

int main()
{
    Truckload load1;  // Create an empty list

    // Add 12 random Box objects to the list
    const size_t boxCount{ 12 };
    for (size_t i{}; i < boxCount; ++i)
        load1.addBox(randomSharedBox()); // random function that generates boxes

    std::cout << "The first list:\n";
    load1.listBoxes();

    // Copy the truckload
    Truckload copy{ load1 };
    std::cout << "The copied truckload:\n";
    copy.listBoxes();

    // Find the largest Box in the list
    SharedBox largestBox{ load1.getFirstBox() };

    SharedBox nextBox{ load1.getNextBox() };
    while (nextBox) // how does this work?!?
    {
        if (nextBox->compare(*largestBox) > 0)
            largestBox = nextBox;
        nextBox = load1.getNextBox();
    }

    std::cout << "\nThe largest box in the first list is ";
    largestBox->listBox();

From the truckload.cpp file the getFirstBox function code is:

SharedBox Truckload::getFirstBox()
{
    // Return m_head's box (or nullptr if the list is empty)
    m_current = m_head;
    return m_current ? m_current->m_box : nullptr;
}

Question: how does the while loop work? Especially the part about 'while(nextBox)' ?
closed with the note: answered

1 Answer

+1 vote
answered Oct 20, 2021 by Peter Minarik (84,720 points)
selected Oct 21, 2021 by Areeb Sherjil
 
Best answer

You have shared a partial code. We do not have the headers RandomBoxes.h and Truckload.h, so one has to make assumptions here.

My assumption is that the type SharedBox is an alias to std::shared_ptr<Box>, where Box is a class that forms the load.

typedef std::shared_ptr<Box> SharedBox;

In the loop, getNextBox() returns a SharedBox and when there is no more Box in the list, it probably returns a null pointer. A null pointer interpreted as a logical expression is false. A non-null pointer interpreted as a logical expression is true.

Therefore

SharedBox nextBox = load1.getNextBox();
while (nextBox)
{
    // ...
}

means: repeat the loop as long as we have a nextBox.

commented Oct 21, 2021 by Areeb Sherjil (1,960 points)
I couldn't really share all the code as there isn't enough space.
But thanks tho!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...