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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:31:36+00:00 2026-05-22T01:31:36+00:00

Is is safe to instantiate a class inside a member function of that class?

  • 0

Is is safe to instantiate a class inside a member function of that class? For example, let’s say I have class CMyClass with member function CMyClass::MemberFunc, and I want to create another instance of CMyClass inside of CMyClass::MemberFunc.

void CMyClass::MemberFunc( void )
{
    CMyClass * pMyClass = new CMyClass();
}

Is this legal/safe? I know it compiles. What I am concerned about is recursion. Will I run into a recursion error when I instantiate CMyClass the first time from the main application?

void main( void )
{
    static CMyClass * s_pMyClass = new CMyClass(); // Will this cause recursion?
}

Or, will recursion only occur only if the specific member function with the additional class instance is called?

void CMyClass::MemberFunc( void )
{
    CMyClass * pMyClass = new CMyClass();
    pMyClass->MemberFunc(); // Pretty sure this will cause a recursive loop.
}

In other words, can I safely instantiate a given class within a member function of that class, so long as I do not call that member function of the second instance of that class? Thanks.

  • 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-22T01:31:37+00:00Added an answer on May 22, 2026 at 1:31 am

    This isn’t more or less safe than instantiating any other object. Note that in your example at the bottom, the recursion is strictly based on the fact that the method calls itself; it would recurse indefinitely regardless.

    In sum: you should be fine.

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

Sidebar

Related Questions

I have a function that accepts a class (not an instance) and, depending on
I have a QThread that contains a QUDPsocket (socket is member not local to
I have a high-level goal of creating a static utility class that encapsulates the
Let's say I'm building a data access layer for an application. Typically I have
I have a class which i realized will not always correctly instantiate and as
We have a source safe server in LAN. I want to connect VSS but
In my widget class that extends extends AppWidgetProvider I have some static final ArrayLists
Is it safe to assume that two itterations over the same collection will return
Is it safe to assume that all implementations of org.springframework.context.MessageSource interface are thread-safe after
Is it safe to assume that event subscribers are called in order of subscription?

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.