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 835381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:49:12+00:00 2026-05-15T04:49:12+00:00

This is really starting to confuse the hell out of me. When do I

  • 0

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?

  • 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-15T04:49:12+00:00Added an answer on May 15, 2026 at 4:49 am

    Declaring an instance of ListOfParts class does not require () when allocating on the heap. Both forms are valid:

    ListOfParts *pPart1 = new ListOfParts();
    ListOfParts *pPart2 = new ListOfParts;
    

    EDIT: As the commenters have pointed out, it makes a difference when initialising a POD type (however is’s not relevant to your code sample).

    However, when declaring a stack variable or a static variable, it matters, because the form with () is the same as declaring a function.

    ListOfParts pPart1(); // a function prototype
    ListOfParts pPart2; // a object construction
    

    const to the right of count() means you cannot modify any values inside the current object in this function, which will be this->size and this->head (note, you can still change object pointed to by head).

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

Sidebar

Related Questions

Probably a really simple one this - I'm starting out with C# and need
I just came across a C++ SDK that makes heavy use of this really
This is really only easy to explain with an example, so to remove the
Okay this is probably a really dumb question, however it's really starting to hurt.
This is starting to vex me. I recently decided to clear out my FTP,
I'm starting to use Linux and Vim at work. I'm started reading vims documentation
This is really strange and I can't seem to figure out why it won't
First off, let me preface this question by stating that I'm really a pretty
This really, really urks me, so I hope that someone can give me a
This really has my stumped today. I'm sure its simple, but... Here is my

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.