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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:37:12+00:00 2026-06-18T04:37:12+00:00

I have the following problem: In myClass I want to default initialize a pointer

  • 0

I have the following problem:
In myClass I want to default initialize a pointer to yourClass, with a new yourClass adress.
Unfortunately, if I want to delete the pointer at any point I get a (core dump).

class myClass
{
      protected:
      yourClass * yc;

      public:
      myClass() { yc = new yourClass(); }

      myClass(yourClass * tyc ) { delete yc; yc = tyc; }

      ~myClass() { delete yc; yc = NULL; }

      void setMyClass (yourClass * tyc) { delete yc; yc = tyc; }

      void print () { yc->print(); }
};

int main()
{
  yourClass b (//parameter);
  myClass * a = new myClass();
  a->print();
  a->setMyClass(&b)
  a->print();

  delete a;
  return 0;
}

The print() of a, should result in two different prints, dependent on //parameters.

I considered yourClass yc; instead of a yourClass* yc, but I want to know if it is possible.

EDIT:
I reworked the code in the following way and it works. Still looks complicated, smart pointers seem promising and I still did not apply the “Rule of Three”.
Here the code. Thanks all.

class myClass
{
      protected:
      yourClass * yc;
      bool dynamic;

      public:
        myClass() { dynamic = true; yc = new yourClass (); }
        myClass (yourClass * tyc ) 
        { 
          // dynamic init (like default)
          if (tyc == NULL ) { dynamic = true; yc = new yourClass (); }
          // static use of yc
          else { dynamic = false; yc = tyc; } 
        }
        // because only if dynamic is true, we need to erase
        ~blu () { if (dynamic) { delete yc; dynamic = false; } } 

        void setMyClass(yourClass* tyc) 
        { 
          // leaving unchanged if new-stuff is NULL or like old-stuff
          if ( tyc == yc || tyc == NULL ) return;
          else // treating dynamic and static differently
          { 
            if (dynamic) // if flag is set, must be deleted 
            {
              delete yc; yc = tyc; dynamic = false;
            }
            else // must not be deleted, dynamic is still false
            {
              yc = tyc;
            }
          }
        }
        void print () { yc->print(); }
};
  • 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-06-18T04:37:14+00:00Added an answer on June 18, 2026 at 4:37 am

    That’s because you are trying to delete too much:

    • you are deleting a non-allocated object in the second constructor (remove delete yc;)
    • you are trying to delete a stack-allocated object, b. delete a; will try to delete a pointer to b, which is an object on the stack; what happens depend on your OS (I expect an exception/core dump/whatever)

    EDIT: another problem I spotted..
    a->setMyClass(NULL)

    I would suggest:

    • this post on smart pointers
    • this blog post on RAII
    • any C/C++ primer explaining stack vs. heap allocation (static vs. dynamic?)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using GWT/JAVA for development. I have following problem: I want to remove
I have the following problem: I want to send a type (java.lang.Class) over the
I have the following problem: @Inject MyClass(Service service) { this.service = service; } public
I have the following Problem with the XMLEncoder. I want to Serialize a Class
I have the following problem: I want to add a custom view (custom_view.xml and
I am completely new to C++\CLI and I have the following problem: I have
I have following problem: I have to make a ASP.NET Webapplication with a two-row
I have following problem: I have built a tabbar application with 4 tabs. I
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
Hi i have following Problem. I write a Mediawiki Extension where i need some

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.