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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:24:14+00:00 2026-05-27T21:24:14+00:00

There is this code: #include <iostream> class CleverClass{ public: CleverClass() : number(55){} void cleverOperation(){

  • 0

There is this code:

#include <iostream>

class CleverClass{
public:
    CleverClass() : number(55){}
    void cleverOperation(){
        std::cout << number << std::endl;
    }
private:
    int number;
};

class NotCleverClass{
public:
    NotCleverClass(CleverClass* cc) : cleverClass(cc){}
    void callCleverOperation(){
            // throw exception when cleverClass object doesn't exist anymore
        cleverClass->cleverOperation();
    }
private:
    CleverClass* cleverClass;
};

NotCleverClass returnNCC(){
    CleverClass CC;
    NotCleverClass NCC(&CC);
    NCC.callCleverOperation(); // prints 55
    return NCC;
}

int main()
{
    NotCleverClass returnedNCC = returnNCC();
    returnedNCC.callCleverOperation(); // prints -858993460
    return 0;
}

Object of class NotCleverClass is dependent to object of class CleverClass. When object of class CleverClass exists then object of class NotCleverClass can use its function cleverOperation() and everything works okey. However, when object of class CleverClass is going to lose existence, then calling its function may cause troubles.

One of the solutions is to keep in NotCleverClass weak pointer (boost::weak_ptr) of CleverClass object with reference checker, but there is still a problem when object of cleverClass is not going to be placed on free store (for example on stack). Are there some design patterns to monitor whether used object still exist and that calling its functions has any sense?

  • 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-27T21:24:14+00:00Added an answer on May 27, 2026 at 9:24 pm

    You can still use a weak_ptr even if the object has automatic storage duration; you can give the shared_ptr a custom deleter that does nothing; and make sure it is destroyed immediately before the object itself, by placing it in the same scope as the object. Something like this:

    class NotCleverClass{
    public:
        NotCleverClass(weak_ptr<CleverClass> cc) : cleverClass(cc){}
        void callCleverOperation(){
            // throw bad_weak_ptr when cleverClass object doesn't exist anymore
            shared_ptr<CleverClass>(cleverClass)->cleverOperation();
        }
    private:
        weak_ptr<CleverClass> cleverClass;
    };
    
    struct null_delete { void operator()(void*) const {} };
    
    NotCleverClass returnNCC(){
        CleverClass CC;
        shared_ptr<CleverClass> shared_cc(&CC, null_delete());
    
        NotCleverClass NCC(shared_cc);
        NCC.callCleverOperation(); // prints 55
        return NCC;
    
        // shared_cc destroyed here: NCC::cleverClass is safely invalidated
        // CC destroyed here: no dangling references remain
    }
    

    This should work with either Boost or C++11 smart pointers. In C++11, you can replace the null_delete functor with a lambda, [](void*){}.

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

Sidebar

Related Questions

There is such code: #include <iostream> class A{ public: friend void fun(A a){std::cout <<
There is this code: #include <iostream> class Base { public: Base(){ std::cout << Constructor
There is this code: #include <iostream> class Bazowa { int x; public: Bazowa() :
There is this code: #include <iostream> class KlasaNiePOD{ public: int a; ~KlasaNiePOD(){} }; int
There is this code: #include <iostream> class SomeClass { int someInteger; float someFloat; public:
There is such C++ code: #include <iostream> class MojaKlasa{ public: int z; void fun(){
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
There is this code: #include <iostream> class Base { int x; }; class Derived
When I compile and run this code: #include <iostream> using namespace std; class Base
There is code: #include <iostream> class Int { public: Int() : x(0) {} Int(int

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.