Click here to Skip to main content
15,569,845 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How, I can prevent an object from being allocated on the heap?

For a class CCard i want to prevent users from doing this

CCard *ptrCard = new CPrintCard;

and only allow them to do this:

CCard objCard;

How can i implement this?
Posted
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 15:39pm    
Good question per se, even though I agree with the concerns about the rationale of this design explained in Answers.
--SA

Answered here. The short version is that it's a "Bad Idea (tm)". Proper documentation is the best way, bsome techniques are shown in this link.

http://stackoverflow.com/questions/10985/how-to-prevent-an-object-being-created-on-the-heap[^]

BTW, I found this with google, and the link above was the 2nd of 166,000.
.
 
Share this answer
 
Comments
CPallini 15-Feb-11 15:21pm    
Well done, 5.
Sergey Alexandrovich Kryukov 15-Feb-11 15:39pm    
Good answer, especially with "short version", my 5.--SA
LaxmikantYadav 15-Feb-11 23:06pm    
Thanks John for your response.
One way can be declaring a
private:
  operator new(size_t);

in the class. No implementation is needed.

Simply an expression like new yourclass dosn't compile since a class new operator is declared but cannot be called, being private.

This is similar as declaring copy and assign as private to disable copy and assignment capabilities produced by default.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 15:40pm    
Good known trick (John's Answer gives more), my 5.
--SA
Emilio Garavaglia 16-Feb-11 15:42pm    
It will be interesting a simple way to admit on-heap allocation, inhibiting on-stack.
(i.e. the inverse of what had been required here). I cannot find a trivial way...
Sergey Alexandrovich Kryukov 16-Feb-11 19:25pm    
Yes, sure, I agree. That's why I appreciated the Question and this simple Answer. Not quite trivial...
Thank you.
--SA
This combination:
C++
template <typename T>
class OnStackType
{
  T m_value;

public:
  OnStackType()                   { m_value = T(); };
  OnStackType(const T& init)  { m_value = init; };
  
  operator T&()               { return m_value; };
  T* operator &()             { return &m_value; };
  T& GetValue()               { return m_value; };
};

class A
{
  int m_iMember;

protected:
  A() : m_iMember(0) {};
  A(const A& a) : m_iMember(a.m_iMember) {};
  ~A() {};

public:
  void SetMember(int iSet) { m_iMember = iSet; };

  friend class OnStackType<A>;
};

- will allow the code: OnStacType<A> a; ,
not: A a; ,
but: OnStackType<A>* pa = new OnStackType<A>(); ... :)
 
Share this answer
 
Comments
Nish Nishant 15-Feb-11 15:40pm    
Well, that uses stack semantics but once the wrapper object is allocated on the heap, the member will also be allocated in the heap. But interesting idea, take a 5 :-)
There's rarely a good reason to do this. So perhaps you need to reconsider your design/requirements.
 
Share this answer
 
Comments
Stephen Hewitt 15-Feb-11 8:25am    
I can think of reasons you may want to do it. For example, to make it harder to misuse some sort of scope guard class.
Nish Nishant 15-Feb-11 8:26am    
Yeah, I did say rarely, but even then there are usually better ways to work around it.
Stephen Hewitt 15-Feb-11 8:32am    
Yeah, you did say rarely. I was wondering if you were going to point that out;)
Nish Nishant 15-Feb-11 8:37am    
:-)
Sergey Alexandrovich Kryukov 15-Feb-11 15:41pm    
Basically agree, My 5. However, the question itself deserves the full answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900