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

The Archive Base Latest Questions

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

After member function delete the current instance,how to stop the behind code executes. See

  • 0

After member function delete the current instance,how to stop the behind code executes.
See the code.

#include <iostream>
class A;
void callfun(int i,A *pt);

class A {
public:
    A() { sss="this is A."; }
    virtual ~A() {}
    void foo(int i) {
        callfun(i,this);  //call a function.Don't return value.Maybe delete instance.
        out();        
    }
private:
    void out() {
        std::cout<< "Out:" <<std::endl;
        std::cout<< sss << std::endl;
    }
    std::string sss;
}

void callfun(int i,A *pt) {
    if (i==0)
        delete pt;  //If conditions are matched,delete instance.
}

int main() {
    A *a1=new A;
    a1->foo(1);     //print:Out:this is A.
    a1->foo(0);     //delete a1,don't print.But in fact it would print.how to do?
}

I want the result:foo(1) output “Out:this is A.”,foo(0) delete instance,don’t output.

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

    The following is a more complete code.

    class A;
    class B {
    public:
      void creat() {
            pp=new A;
        }
        void remove() {
            delete pp;
            pp=NULL;
        }
    private:
        A *pp;
    };
    
    class A {
    public:
        void foo(int i) {
            callfun(i,this); 
        if (flag)
                out();      
            else 
                bb->remove();
        }
    private:
        B *bb;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: delete heap after returning pointer I have a class with a member
Inside of a static member function I need to get the type. class MyClass
I have a situaion in which I want to declare a class member function
I just wrote a sample program to see the behaviour of delete this class
I am writing a class in which I want to create member function template
After I click update in the grid view, the code works successfully. But when
I've got this function: template<typename T> void Inventory::insertItem(std::vector<T>& v, const T& x) { std::vector<T>::iterator
In a recent project I had to create a Singleton class and after a
Currently I'm trying to set up a member function for Student that reads a
I need to call function whenever class is created using new. Currently I need

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.