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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:14:07+00:00 2026-06-07T14:14:07+00:00

Well, I need to return a pointer to an instance of a class that

  • 0

Well, I need to return a pointer to an instance of a class that will be created inside a function. Is this appropriate?

this is example code:

template <typename T>
ImplicatedMembershipFunction<T>* 
TriangularMF<T>::minImplicate(const T &constantSet) const
{
    static ImplicatedType* resultingSet = new ImplicatedType();
    // do something to generate resultingSet...
    return resultingSet;
}

I want to return pointers, because need to have subclasses of a base class in a container. In the above code ImplicatedType is a class defined in TriangularMF<T> and derived from ImplicatedMembershipFunction<T>. There will be various template classes like TriangularMF that the have a nested class derived from ImplicatedMembershipFunction<T>, I need to treat with them in same way. For example, outside the library, I may want to do something like :

TriangularMF<double> trmf(0,1,2);
TrapesoidalMF<double> trpmf(0,1,3,2); // a class like TriangularMF but
                                      // ImplicatedType is different 
ImplicatedMembershipFunction<double>* itrmf = trmf.implicate(0.6);
ImplicatedMembershipFunction<double>* itrpmf = trpmf.implicate(0.6); // same as above.

// use them in the same way:
vector<ImplicatedMembershipFunction<double>*> vec;
vec.push_back(itrmf); 
vec.push_back(itrpmf);

The reason that I don’t want to use C++11 features like move semantics or std::shared_ptr is that I don’t like to force my teammates to install newer versions of g++ on their computers. I can’t give them a compiled version of the library, because it’s heavily templated.

EDIT
The library is going to be threaded. Especially, the TriangularMF<T>::minImplicate will run in multiple threads at same time. So, making the minImplicate a mutal task, makes no sense for the performance.

  • 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-07T14:14:09+00:00Added an answer on June 7, 2026 at 2:14 pm

    Returning a pointer is not itself the issue, but you have to define a clean “policy” about whoi creates and who destroy.

    In your code, you define a static pointer that is initialized with a new object the very first time its (pointer) definition is encountered.

    The pointer itself will be destroyed just after main() will return, but what about the object it points to?
    If you let something else to take care of the deletion, your function will continue to return that pointer even if the object is no more there. If you let it there, it will be killed out at the end of the program (not a “dangerous” leak, since it is just one object, but what about if its destructor has to take some sensible actions?)

    You have most likely to declare, not a static pointer, but a static OBJECT, and return … its address or its reference.

    In that way the object is granted to exist up to program termination and to be properly destroyed after main() returns.

    template <typename T>
    ImplicatedMembershipFunction<T>* 
    TriangularMF<T>::minImplicate(const T &constantSet) const
    {
        static ImplicatedType resultingSet(....);
        return &resultingSet;
    } 
    

    Note that I eliminated your “do something to …” since it will be executed every time (not just the very first) To initialize ImplicatedType, you had better to rely on the constructor.
    Or, if you cannot construct it in one shot, do something like

    template <typename T>
    ImplicatedMembershipFunction<T>* 
    TriangularMF<T>::minImplicate(const T &constantSet) const
    {
        static ImplicatedType* resultingSet=0;
        static bool init=true;
        if(init)
        {
            init=false; 
            static ImplicatedType result;
            resultingSet=&result;
            // do something to generate resultingSet...
        }
        return resultingSet;
    }
    

    If you are in a multithreading situation, you also need a static mutex an lock it before if(init), unlocking at return.

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

Sidebar

Related Questions

I need a regular expression that will accept well-formed emails in several formats (see
I need to write code for a callback function (it will be called from
Well i need some help here i don't know how to solve this problem.
I need a list of any well known Linux applications that uses object detection
I need a regular expression that should validate decimal point as well as range.
Imagine that I have a Debtor class. With Hibernate, I will define the class
I have a function that computes and create a matrix. My function works well
I have a javascript function (class) that takes a function reference as one paremter.
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
I understand very well the need for websites' front ends to be coded and

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.