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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:45:56+00:00 2026-05-25T19:45:56+00:00

This may be impossible, but I was wondering if it was possible to keep

  • 0

This may be impossible, but I was wondering if it was possible to keep a temporary from ever lasting past its original expression. I have a chain of objects which point to parent objects, and a member function which will create a child object, a simplified example is here

class person{
    string name;
    person * mommy;
public:
    person(const string & nam, person * m = 0) : name(nam), mommy(m) {}
    person baby(const string & nam){
        return person(nam, this);
    }
    void talk() const{
        if (mommy) mommy->talk();
        cout << name << endl;
    }
};

int main(){
    person("Ann").baby("Susan").baby("Wendy").talk();     // fine

    const person & babygirl = person("Julie").baby("Laura"); // not fine

    babygirl.talk();    // segfault
    return 0;
}

The way I want to use person is to pass it to a function, and something like this:

void use(const person & p) {
    p.talk();
}
use(person("Anna").baby("Lisa"));

Is fine.

This will work fine as long as none of the temporaries survive past the original expression, but if I bind one of the final temporaries to a const reference, its parents don’t survive, and I get a segfault. I can hide person‘s copy constructor and assignment operator, but is there any way I can prevent this kind of error from happening? I’d like to avoid dynamic allocation if possible.

  • 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-25T19:45:57+00:00Added an answer on May 25, 2026 at 7:45 pm

    It looks like you are creating a data structure here where children have pointers to their parents. Using temporaries is guaranteed to cause you grief in this case. In order to make this safe, you will need to dynamically allocate and possibly use some sort of reference counting.

    Have you considered using boost::shared_ptr? It is an implementation of a reference counted smart pointer class. Using shared_ptr and perhaps some factory methods, you might be able to get the effect you want and reduce the pain of dynamic memory allocation. I tried it out and it seems to work. Once the code exits the scope, the objects are all destroyed because there are no references left to the shared_ptrs.

    Edit:
    In response to zounds’ comment, I have modified the example so that the root object controls the data structure’s lifetime.

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost\shared_ptr.hpp>
    #include <boost\weak_ptr.hpp>
    
    using boost::shared_ptr;
    using boost::weak_ptr;
    
    using std::string;
    using std::cout;
    using std::endl;
    using std::vector;
    
    class person;
    typedef shared_ptr<person> Person;
    typedef weak_ptr<person> PersonWk;
    
    class person {    
        PersonWk pThis;
        friend Person makePerson(const string & nam, Person m = Person());
    
        string name;
        PersonWk mommy; // children should not affect parent lifetime, so store weak ptr
        vector<Person> children; // parents affect children lifetime so store child shared ptrs
    
        // make constructor private so that you can only make a person using factory method
        person(const string & nam, Person m) : name(nam), mommy(m) 
        { 
            // for demo purposes
            printf("creating %s\n", nam.c_str());
            ++personCount; 
        }
    
        // undefined copy constructor and assignment operators
        person(const person&);
        person& operator=(const person&);
    
    public:
        // for demo purposes
        static int personCount;
    
        ~person() 
        { 
            // for demo purposes
            printf("destroying %s\n", name.c_str());
            --personCount; 
        }
    
        Person baby(const string & nam){        
            Person child = makePerson(nam, Person(pThis));
            children.push_back(child);
            return child;
        }
    
        void talk() const{
            if (Person mom = mommy.lock()) 
                mom->talk();
            cout << name << endl;
        }
    };
    
    int person::personCount = 0;
    
    // factory method to make a person
    Person makePerson(const string & name, Person m) {
        Person p = Person(new person(name, m));
        p->pThis = p; // stash weak_ptr so I can use it to make a shared_ptr from "this" in the baby method
        return p;
    }
    
    void use(const Person p) {
        printf("In method use...\n");
        p->talk();
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        printf("personCount=%d\n", person::personCount);
        {
            Person ann = makePerson("Ann");
    
            // ann has baby and grandbaby, pass grandbaby to use method
            use(ann->baby("Susan")->baby("Wendy"));
    
            ann.reset(); // remove reference from root object. Destruction ensues...
        }
        printf("personCount=%d\n", person::personCount);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a daft question, but i was wondering about how to
This may be impossible, but I'm going to ask anyway. Without going into the
This may be a stupid question, but is it possible to capture what a
This may be a no-brainer for the WPF cognoscenti, but I'd like to know
This may be simple one, but 5 mins of Googling didn't give me the
This may be a simple fix - but I'm trying to sum together all
This may be a matter of style, but there's a bit of a divide
This may sound strange but sometimes when your ASP.NET webapp isn't working and you
This may be a doozy, but does anyone have an idea how to: Pass
This may be an oxymoron, but how would one update a data entity in

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.