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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:57:12+00:00 2026-06-18T01:57:12+00:00

I have a class that I only wish clients create them one object per

  • 0

I have a class that I only wish clients create them one object per process. Instead of singleton, a better way (I believe) is to tell the clients to only create them in main(). So a natural enforcement is to make the constructor private and main() as a friend.

It works this way:

class A { friend int main(int, char**); A() {} };
int main(int, char **) { A a; }

But it breaks when I need to put class A in a namespace:

namepace ns { class A { friend int main(int, char**); A() {} }; }
int main(int, char **) { ns::A a; }

The problem is scoping: the compiler now thinks

friend int main 

means a function named main() in namespace ns. So the real main() becomes irrelevant.

So the question is: how to fix this? Of course I’ll have to put class A in a namespace.

  • 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-18T01:57:14+00:00Added an answer on June 18, 2026 at 1:57 am

    You need to declare main in the global namespace before the class definition, since friend declarations can only introduce names in the surrounding namespace:

    int main(int, char**);
    

    and qualify the name when referring to it inside the namespace:

    namepace ns { class A { friend int ::main(int, char**); A() {} }; }
    //                                 ^^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two methods on a class that differ only in one method
I have a class in an assembly that I wish to be a singleton.
I have a helper class that should only ever run in a background thread.
I have a CORE class that pertains only to my specific site, ie, it
Given: I have an interface. I have only class that implements that interface. Question:
I have a class called Property that I only need to display a few
I have a custom class that uses boost mutexes and locks like this (only
I have method in a class that I need to make sure is only
I have a custom class that I wish to save and load. The class
I have a class template similar to the one that follows below that is

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.