Roger
Roger

Reputation: 2963

Difficulty implementing a linked list in C++

I am trying to implement a linked-list in C++. Currently, I have the following code:

using namespace std;

struct CarPart
{
    int partNumber;
    char partName[40];
    double unitPrice;

    CarPart* Next;
};

class ListOfParts
{
private:
    int size;
    CarPart* Head;
public:
    ListOfParts():size(0), Head(NULL)
    {    
    }

    int Count()
    {
        return size;
    }
};

Here the problem is, ideally, I should keep the Stuct CarPart within my Class. But I do not want to. At the same time, I don't want this to be acccessble anywhere from outside.

Can I have a some way, without creating a structure within the Class? Instead creating a new Class CarPart which could be accessible from only class ListOfPart?s

Upvotes: 0

Views: 422

Answers (3)

Liam M
Liam M

Reputation: 5432

Well, as a first suggestion, have you considered using std::list? It would save you the trouble of implementing your own linked list semantics. Unless you're writing a linked list for the learning experience (which can be valuable), I suggest using:

struct CarPart
{

    int partNumber;
    std::string partName;
    double unitPrice;
};

std::list<CarPart> ListOfParts;

You'll also notice I'm using std::string for text, which I suggest you use (unless you have a very good reason not to).

To the question at hand: you could declare the constructor for CarPart private, and then declare ListOfParts as a friend class, that's one way. But consider this: what do you gain by disallowing the construction of a car part external to the list of parts? I can't see that you gain anything. In fact, by using friends you introduce unnecessary complexity into the structure of your code - as using the dreaded 'friend' keyword usually does. Anyway, if you did want to use the friend class method, you would write:

class ListOfParts;
struct CarPart
{
    friend class ListOfParts;

    int partNumber;
    char partName[40];
    double unitPrice;
    CarPart* Next;

private:

    CarPart()
    {
        // Do nothing.
    }
};

Which would mean only ListOfparts could call the default constructor for the list CarPart. Let me make this very clear: this is an abhorrent solution because it breaks rules of encapsulation. But, like mutable, friends have a use (and this isn't it).

Upvotes: 3

Oleksandr Pryimak
Oleksandr Pryimak

Reputation: 1581

You can move your CarPart struct to a separate header and include this header only in the ListOfParts implementation part (yes, you need to separate definitions from implementations).

And don't forget a forward declaration

struct CarPart

before defining

class ListOfParts

Upvotes: 0

Komi Golov
Komi Golov

Reputation: 3471

What you're asking is contradictory. Either you want CarPart to be accessible from outside (in which case you declare it as a separate class or as a public member) or you don't want it accessible (in which case you declare it as a private member).

Consider making your class a little more generic: instead of having it be a linked list of CarParts, make it a class template that makes a linked list of Nodes that each has a T. If you are allowed to, you should be using std::list anyway, but you could write your own if you had to/really wanted to.

Also, classes and structs are basically the same thing; the only difference is that class members and inheritance are by default private, and struct members and inheritance are by default public. (The keywords are not always interchangeable, though.)

Upvotes: 0

Related Questions