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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:05:42+00:00 2026-05-16T08:05:42+00:00

In this contrived example, I have a static initialization function that is run at

  • 0

In this contrived example, I have a static initialization function that is run at construction time.

I’d like to know if this a legal way to initialize a_, b_, and c_. Or, is it too early in the construction process to use them this way?

DWORD SomeInitializationFunction( HANDLE*, DWORD*, DWORD* );

class MyClass
{
public:
    MyClass() : m_( MyClass::Create( &a_, &b_, &c_ ), &::CloseHandle )
    {
    };

private:

    static HANDLE Create( DWORD* a, DWORD* b, DWORD* c )
    {
        DWORD a1, b1;
        HANDLE h;

        *c = ::SomeInitializationFunction( &h, &a1, &b1 );
        if( *c == 0 )
        {
            *a = a1;
            *b = b1;
            return h;
        }
        return NULL;
    };

    boost::shared_ptr< void > m_;
    DWORD a_;
    DWORD b_;
    DWORD c_;
};

Thanks,
PaulH

  • 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-16T08:05:43+00:00Added an answer on May 16, 2026 at 8:05 am

    Members in a class are initialized in the order they are defined in the class. Thus if you have

    class A
        int t
        int u
        int v
    A() : v(0), u(1), t(2) {}
    

    Then despite the order in which you write the constructor arguments, first the value of t would be set, then the value of u and finally the value of v.

    So if you change the order of definition of your class to:

    class MyClass
    {
    public:
        MyClass() : m_( MyClass::Create( &a_, &b_, &c_ ), &::CloseHandle )
        {
        };
    
    private:
    
        static HANDLE Create( DWORD* a, DWORD* b, DWORD* c )
        {
            DWORD a1, b1;
            HANDLE h;
    
            *c = ::SomeInitializationFunction( &h, &a1, &b1 );
            if( *c == 0 )
            {
                *a = a1;
                *b = b1;
                return h;
            }
            return NULL;
        };
    
    
        DWORD a_;
        DWORD b_;
        DWORD c_;
        boost::shared_ptr< void > m_;
    };
    

    then your constructor should be okay.

    I’d suggest putting in a comment saying that there is a dependency in the initialization of the members of your class so that people reading the code would know not to fiddle around.

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

Sidebar

Related Questions

This is a contrived example: public static class MyExtensions { public static void MyMethod(
This may seem like a somewhat contrived example, but I'm left scratching my head.
This is a contrived example, but it illustrates my problem much more concisely then
Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar
I have a WTL 8.0 SDI application for Windows Mobile 5. In this contrived
So I have this ASP.Net 2.0 website that uses functions contained within a JS
I have a function I'd like to transform into a LINQ to SQL expression,
Is there any good way to unit test destructors? Like say I have a
I have an interface - here's a nicely contrived version as an example: public
I have class like this class variable { public: variable(int _type=0) : type(_type), value(NULL),

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.