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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:18:20+00:00 2026-06-15T18:18:20+00:00

I have the following problem. I create a class, and store the pointer to

  • 0

I have the following problem. I create a class, and store the pointer to that class in an other class. Upon creating, everything is OK. However, a single step later it seems that the class has disappeared.

I’ve written a very simple test scenario here:

#include <iostream>

using namespace std;

class test
{
    public:
        test();
        bool ok;
};

test::test()
{
    ok = false;
}

class func
{
    public:
        func();
        void check();
        test *pTest;
};

func::func()
{
    test temptest = test();
    cout << temptest.ok << endl;
    pTest = &temptest;
    cout << pTest->ok << endl;
}

void func::check()
{
    cout << pTest->ok << endl;
};

int main( int argc, char *argv[] )
{
    func mFunc = func();
    // what happens here
    mFunc.check();
}

The above program outputs the following:

0
0
204

From 204 I’d guess that somehow the class I’ve created before has disappeared.

Can you tell me what is happening and why?

  • 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-15T18:18:22+00:00Added an answer on June 15, 2026 at 6:18 pm

    The problem is that you are creating a an object with a “limited” scope.

    func::func()
    {
        test temptest = test();             // temptest construction
        cout << temptest.ok << endl;
        pTest = &temptest;
        cout << pTest->ok << endl;
    }                                       // temptest descrution 
    

    After the construction of func, pTest now refer to an invalid object.

    You have to use dynamic memory or shared pointers to manage pointers.

    #include <iostream>
    
    using namespace std;
    
    class test
    {
        public:
            test();
            bool ok;
    };
    
    test::test()
    {
        ok = false;
    }
    
    class func
    {
        public:
            func();
           ~func();
            void check();
            test *pTest;
    };
    
    func::func()
    {
        pTest = new Test();
        cout << pTest->ok << endl;
        cout << pTest->ok << endl;
    }
    func::~func() { delete pTest; }
    
    void func::check()
    {
        cout << pTest->ok << endl;
    };
    
    int main( int argc, char *argv[] )
    {
        func mFunc = func();
        // what happens here
        mFunc.check();
    }
    

    Now the constructor of test allocate a new object and store the address of that object, and the destructor can deallocate memory. Manage memory in this way is not a good prectice.

    Use shared pointers instead like shared_ptr or unique_ptr, but this requires some other knowledge such move semantic.

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

Sidebar

Related Questions

i have the following problem: i would like to create a footer. that footer
I have the following problem: Within a stylesheet document I want to create an
The problem is the folowing: I have to create a panel in Vaadin that
I have defined the following ApplicationManifest file the problem is that i have created
My problem is similar to: Problem creating NSManagedObject derived class I have setup a
I have the following problem. I have a Preferences Page that stores preferences using
I need help with the following problem: I have a class with two properties.
I have the following problem. I got a class PluginLoader which oversees loading of
Im facing following problem I have created mentioned condition, but when I choose y
i'm having a problem with my database. I have the folowing: create table book

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.