0

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

5
  • Just for your information, and if your project is getting held up over this: C++ already has a singly-linked list in <forward_list> which should meet all your needs. You can relax and tell your boss you "solved it" and take the weekend off! :-) Commented Jan 14, 2012 at 14:05
  • @kerek SB: Thanks a lot to chip in with a nice suggestion. I remember I interacted with u..I am just a learner, have been with C++ for sometime..finally decided need to play with it n Explore all the possible ares!! Commented Jan 14, 2012 at 15:14
  • Very good. My suggestion would be to either bite the bullet and do make CarPart a nested class, for now, and for learning only, and when you got it to work, you make the actual type a template and use a generic "node" class, which you keep as a nested class. Commented Jan 14, 2012 at 15:18
  • actually I just got this one worked: I made a struct CarPArt as extenal to the class. I doubt this method follows encapsulation. But a nested class seems to be a better solution. Why is that not a good one? Commented Jan 14, 2012 at 15:22
  • For now you won't really see the need for this, but eventually you will realize that all linked lists have the same pattern, and the only difference is the payload of the node. Once you get tired of copy/pasting nodes, you'll make one single linked list template, and the node naturally becomes a nested class which has no use outside the list. Commented Jan 15, 2012 at 0:33

3 Answers 3

3

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).

Sign up to request clarification or add additional context in comments.

4 Comments

u mean something like this: class CarPart{ int i; Friend class ListofParts; Carpart* next; private: CarPArt() { } } class listofParts{ { CarPart *head; }
@Roger see my amendments above.
but even the method I showed in my question earlier..thatis, with the Struct outside of class; even this would not be following encapsulation, right?? So what would be the apt solution ?
@Roger The best way to achieve what you're trying to without using friends, is a nested class.
0

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.)

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.