When to use () with classes?

Posted by SoulBeaver on Stack Overflow See other posts from Stack Overflow or by SoulBeaver
Published on 2010-06-06T14:05:16Z Indexed on 2010/06/06 14:12 UTC
Read the original article Hit count: 134

Filed under:
|
|

This is really starting to confuse the hell out of me. When do I use them, when don't I?

For example I was reading a .cpp on linked lists whose class declaration was:

struct CarPart
{
    long PartNumber;
    char Partname[40];
    double UnitPrice;

    CarPart *next;
};

class ListOfParts
{
    int size;

public:
    CarPart *head;

    ListOfParts();
    ~ListOfParts();

    const int count() const;
    void insert( CarPart *item );
    CarPart *retrieve( int pos );
};

With this code, why am I allowed to write

ListOfParts *pPart = new ListOfParts();
CarPart *pCarPart = new CarPart;

Declaring an instance of ListOfParts requires (), but not my CarPart? That's confusing me. When I asked a question before and people told me that such a declaration is a function that returns a ListOfParts object, but not the actual constructor. So I'm guessing this is still something different.

What's happening here?

PS: Am I correct to assume that the const to the right of count() means I cannot modify any values in count?

© Stack Overflow or respective owner

Related posts about c++

Related posts about function