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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:59:51+00:00 2026-05-22T15:59:51+00:00

So, i found different ways to implement the creation of the singleton. EDIT: When

  • 0

So, i found different ways to implement the “creation” of the singleton.
EDIT: When I say “creation”, I mean it. This code would be placed in the Singleton::{ctor} or static Singleton::Init() functions of course.

//v1
        //the first "1" is casted to a pointer to Ty, then that pointer is casted
        //back to int to obtain the hex address
        //the second 1 is casted to a pointer to Ty, then to a pointer
        //to cSingleton<Ty> because Ty is a derived class and finally
        //back to int to get the hex address
        //after that it's simple pointer arithmetics to get the offset
        int offset = (int)(Ty*)1 - (int)(cSingleton <Ty>*)(Ty*)1;
        m_pSingleton = (Ty*)((int)this + offset);
//v2
        m_pSingleton = static_cast<Ty*>(this);
//v3
        m_pSingleton = (Ty*)this;

Is there any significant difference between them?
To my knowledge, v2 and v3 should be the same, but it’s v1 I don’t really understand. I kinda know what it does, but for what purpose?

Also, please don’t turn this into a “Singletons are BAAAAD” discussion.

  • 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-22T15:59:52+00:00Added an answer on May 22, 2026 at 3:59 pm

    (Since the question seems to have died, I’ll try to answer it on my own.)
    What v1 does is manually adjusting the this pointer to point to the address of the derived object. Normally, static_cast or a normal c-style cast does this by itself, but maybe that wasn’t the case on earlier compilers or there was a bug. What ever is the case, it does what the casts do.

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

Sidebar

Related Questions

I found some exercises where you combine n-bit 2's complement values in different ways
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
I found this open-source library that I want to use in my Java application.
I found the ad on this site to Krypton controls ( and here's another
I found a question here about blogs on software development, but I would like
I found this link http://artis.imag.fr/~Xavier.Decoret/resources/glsl-mode/ , but there isn't a lot of description around
I've been looking at ways to implement gmail-like messaging inside a browser, and arrived
Edit: This question was written in 2008, which was like 3 internet ages ago.
I'm looking for the different ways to map an enum using JPA. I especially
I have a some classes which I need to serialize in two different ways:

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.