0
class K {
public:

    K(int a, int n) : n(n) {
    }


    static void allocate(unsigned number, unsigned n) {
        K* w = reinterpret_cast<K*> (::operator new( sizeof (K) * number)); 

        for (int i = 0; i < number; i++) {
            new( (void*) (w + i))K(24, n);
            //(w)->K::K(24,n);

        }
    }
private:


    const int n;

};
  1. Why the first way ( new( (void*) (w + i))K(24, n);) don't make error and the second way ((w)->K::K(24,n);) make error? Eventually, how repair it?
  2. Tha allocate is static, so why I have access to n which is private?
2
  • 2
    Why do you keep doing crazy things with reinterpret_cast and calls to operator new? Commented Mar 16, 2014 at 23:08
  • help me, I mean you say me how to realize it correctly. Commented Mar 16, 2014 at 23:15

2 Answers 2

3

I'm going to state my understanding of what you're trying to do, which is not 100% clear to the respondents. Since you don't have a default constructor for your class, you cannot allocate them in an array using new[]. Your recourse is to invoke the constructors manually using placement new.

In general, try to make a default constructor for classes that need to be allocated with new[]. Then you can just go update them after you've allocated them. This is simpler and easier to read. For example, w = new K[number]; and delete[] w; handles everything for you, but it can only call the default constructor. So you must do "post construction initialization" with your own function to set them up the way you want.

If you really want to call the individual constructors using placement new, you can and it is safe. It's not my recommended design, but neither is it as evil as some might assume. Your code works, but you don't do anything with w after you're done, so it's a leak... but examining it in the debugger, it has called the constructor on each one, just like you wanted. You should note that you are reusing n in different scopes, which is confusing. You should disambiguate that. The n from allocate is being fed into each constructor.

One other important note though, and this is the disadvantage of the design of using placement new here. You must:

  1. Manually invoke the destructor on each element when you are done. Therefore, you must track the length of the array manually until freeing.
  2. Free w the matching way it was allocated. Do not use delete[] if you didn't use new[].
Sign up to request clarification or add additional context in comments.

Comments

0

static members may access the private data of any instance of its type. Dem's da rules.

However, here, you're using the function argument.

I'm not even going to comment on your mental memory allocations.

4 Comments

isnt n hidden by the n passed to the function?
@thumbmunkeys: That is also true :)
"I'm not going to comment on your mental memory allocations." Help :)
@user3425832: Don't do them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.