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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:28:09+00:00 2026-06-05T01:28:09+00:00

Suppose we have a class as class Egg { static Egg e; int i;

  • 0

Suppose we have a class as

class Egg
{
    static Egg e;
    int i;
    Egg(int ii):i(ii) {}
    Egg(const Egg &);    //Prevents copy-constructor to be called
  public:
    static Egg* instance() {return &e}
};

Egg Egg::e(47);

This code guarantees that we cannot create any object, but could use only the static object. But how could we declare static object of the same class in the class.

And also one thing more since e is a static object, and static objects can call only static member functions, so how could the constructor been called here for static object e, also its constructors are private.

  • 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-05T01:28:12+00:00Added an answer on June 5, 2026 at 1:28 am

    But how could we declare static object of the same class in the class.

    A static member variable is not stored inside each object of a class. So if you declare a static member variable inside a class or as a namespace level object after you defined the class, differs only in respect to access (Class::var and var) and access to protected and private members.

    And also one thing more since e is a static object, and static objects can call only static member functions

    I think you are mixing static functions and static objects. Inside a static function you can call only static functions (unless you are calling them on an object).

    so how could the constructor been called here for static object e

    Like for every other object a constructor has to be called for static objects, too.

    also its constructors are private

    Access Control is checked on class level in C++. So since the static object is inside the class, it can access private members.

    Unlike in some other languages, the following is legal in C++, since the access to a private member is from inside the class – even if on another object (other in this case):

     class Test {
     private:
          int i;
     public:
          Test(const Test &other)
          : i(other.i)
          {}
     };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose you have a class NonCopyable class NonCopyable { public: NonCopyable(int n){} ~NonCopyable(){} [...]
Suppose I have: public class Bob { public int Value { get; set; }
Suppose I have a class like the following: public class Stage { public int
Suppose I have a class with a factory method class A { public: static
Suppose I have a class called Animal and an interface called AnimalTrainer. public interface
Suppose I have class like class A{ public: A(int a, boost::shared_ptr<int> ptr){ // whatever!
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose we have: class Base { __forceinline virtual int A() {return 1;} } class
Suppose I have this class: public class DispatcherService<T> { private static Action<T> Dispatcher; public

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.