Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7061305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:27:28+00:00 2026-05-28T04:27:28+00:00

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

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:27:29+00:00Added an answer on May 28, 2026 at 4:27 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a linked list using following code, I got segment
I have been trying to implement my own linked list class for didactic purposes.
I'm having trouble trying to implement a linked List without using classes(we're not there
I am trying to implement a simple linked list using c++. I am probably
I wrote the following code when trying to make a doubly-linked list with an
I am trying to implement the linked list data structure using java, it works
I have been trying to implement XOR linked list and its operations but I
Here is code in which I am trying to implement a queue using linked
I'm trying to implement a linked list but get an error when compiling: intSLLst.cpp:38:
I'm trying to implement a linked list abstraction, however I am running into problems.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.