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

  • Home
  • SEARCH
  • 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 751501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:40:26+00:00 2026-05-14T14:40:26+00:00

As I understand it, when a new class is instantiated in C++, a pointer

  • 0

As I understand it, when a new class is instantiated in C++, a pointer to the new class is returned, or NULL, if there is insufficient memory. I am writing a class that initializes a linked list in the constructor. If there is an error while initializing the list, I would like the class instantiator to return NULL.

For example:

MyClass * pRags = new MyClass;

If the linked list in the MyClass constructor fails to initialize properly, I would like pRags to equal NULL. I know that I can use flags and additional checks to do this, but I would like to avoid that, if possible.
Does anyone know of a way to do this?

  • 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-14T14:40:27+00:00Added an answer on May 14, 2026 at 2:40 pm

    The common approach here is to throw an exception (and handle it somewhere higher up).

    One of the benefits of the exception mechanism is that it allows you to throw an exception from within a class constructor. In that case, you never reach the situation where a pointer is returned to the invalid. You would “get control” at the corresponding catch block. If the pointer was only declared within the try block (or in some other method invoked by the try block), it would be outside your scope in that catch block.

    That is not a hack – it is quite legitimate and a common programming technique.
    For example, if your class constructor allocates memory dynamically (e.g., for an internal buffer) and this allocation failed, you would want to throw an exception because at the end of the constructor body you would not have a valid object.

    Here is an example (I’ve been doing Java for the past 10 years, so my C++ code below is likely messed up here, maybe someone can edit this for me)

    // Begin C++ code written by a Java programmer... :)
    class Myclass
    {
       public:
          Myclass(int length)
          {
              if(length<=0) throw BadBufferSizeException("Bla bla bla");         
              this->buffer = (char*)malloc(length*sizeof(char)); // don't remember the new syntax
          }
    
          void doSomething()
          {
              // Code for placing stuff in the buffer
          }
        private:
          char* buffer;
    };
    
    
    int main()
    {
       try
       { 
         int len;
         len = getLengthFromUser();
         MyClass* pMyClass = new MyClass(len);
         myClass->doSomething();
        } catch(const Exception & e)
        {
           // Whatever... Note how pMyClass is not even accessible here
        }
    }
    

    Note that if you defined pMyclass to be null outside the try block, and then only reassigned it within the try block when you create the class, in the case of failure you would likely still have null, but you would never have executed doSomething(). If you are concerned about initialization, you could also move the doSomething() call to outside the try-catch block, but you would want to make sure that your pointer is not null.

    Also note that C++ gives you more (too much?) freedom when it comes to throwing things than other languages. I usually like having a hierarchy of exception classes or using an existing library with such a hierarchy.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the zeroclipboard library. May 14, 2026 at 7:19 pm
  • Editorial Team
    Editorial Team added an answer Using CSS, of course... label { display: block; width: 100px;… May 14, 2026 at 7:19 pm
  • Editorial Team
    Editorial Team added an answer I believe .live() is more like .bind() than it is… May 14, 2026 at 7:19 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.