Jump to content
  • Advertisement
Sign in to follow this  
badmoon

Interview question: "How do you force an empty class to be allocated in the heap?"

This topic is 5465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got this question asked in a job interview. But was rather confused as it's the instances of classes you allocate in the heap, not the class itself right? I mean the class just exists in the code segment of memory. Can anyone answer this question

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Make all constructors private, and create a public factory interface
via static member functions that only instantiate the class
on the heap. If a client wants an instance, it's only going
to get one via the factory interface.

Prevents subclassing, though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Make all constructors private, and create a public factory interface
via static member functions that only instantiate the class
on the heap. If a client wants an instance, it's only going
to get one via the factory interface.

Prevents subclassing, though.


Whats stopping someone from overriding the global operator new allocation function to allocate the object from some non-heap based resource?

Share this post


Link to post
Share on other sites
Why doesn't the new operator allocate on the heap?



my_class *p = new my_class();


Share this post


Link to post
Share on other sites
Quote:
Original post by Jingo
Quote:
Original post by Anonymous Poster
Make all constructors private, and create a public factory interface
via static member functions that only instantiate the class
on the heap. If a client wants an instance, it's only going
to get one via the factory interface.

Prevents subclassing, though.


Whats stopping someone from overriding the global operator new allocation function to allocate the object from some non-heap based resource?

Because he made his constructor private. It doesn't matter if you globally overload new, the constructor still can't be called so you'll get an error. No one can construct an instance of the type (whether via new or otherwise) unless the function is a part of the class. It's up to however he defines his allocation within the function (whether via a local new or his own dynamic memory allocation or malloc and placement new, etc).

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
Quote:
Original post by Jingo
Quote:
Original post by Anonymous Poster
Make all constructors private, and create a public factory interface
via static member functions that only instantiate the class
on the heap. If a client wants an instance, it's only going
to get one via the factory interface.

Prevents subclassing, though.


Whats stopping someone from overriding the global operator new allocation function to allocate the object from some non-heap based resource?

Because he made his constructor private. It doesn't matter if you globally overload new, the constructor still can't be called so you'll get an error. No one can construct an instance of the type (whether via new or otherwise) unless the function is a part of the class.


Since when does the operator new allocation function attempt to call a constructor?

Share this post


Link to post
Share on other sites
I agree with smr - I seem to be missing something here. Washu - could you explain why you can't?
the operator new always returns a unique address, even for a 0 byte allocation (my version of the CRT allocates 1 byte instead of 0), so allocating a new object of zero bytes still gives you an address on the heap doesn't it?

As I said, I think I'm missing something...

Share this post


Link to post
Share on other sites
Quote:
Original post by Jingo
Quote:
Original post by Polymorphic OOP
Quote:
Original post by Jingo
Quote:
Original post by Anonymous Poster
Make all constructors private, and create a public factory interface
via static member functions that only instantiate the class
on the heap. If a client wants an instance, it's only going
to get one via the factory interface.

Prevents subclassing, though.


Whats stopping someone from overriding the global operator new allocation function to allocate the object from some non-heap based resource?

Because he made his constructor private. It doesn't matter if you globally overload new, the constructor still can't be called so you'll get an error. No one can construct an instance of the type (whether via new or otherwise) unless the function is a part of the class.


Since when does the operator new allocation function attempt to call a constructor?

"new" doesn't -- the expression does. When you call new, the constructor of the object must be called (the default constructor implicitly called if you don't specify parameters. If it is private, you can't have an expression with new outside of the class.

Share this post


Link to post
Share on other sites
Yeah but that's forcing an instance of a class to be allocated in the heap. The interviewer seemed to want to know how you'd force a class to be allocated in the heap. Does this make sense to anyone?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!