By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

friend operator new/delete - allow obj instantiation only in heap - howto?

P: n/a
Hi all,
All I want to achieve is restricting the object instantiation in stack
and allowing the application to instantiate the object only in heap
using new operator. How to achieve this?

I tried out with the following code.
Could you please give me the reason for the compilation error for the
following code:

#include <iostream>

class Temp
{
public:
friend void* ::operator new (size_t);
friend void ::operator delete (void* pDel);
private:
Temp ()
{
std::cout << "Temp Ctor" << std::endl;
}
~Temp ()
{
std::cout << "Temp Dtor" << std::endl;
}
};

int
main ()
{
Temp *pT = new Temp;
delete pT;

// Temp tempObj; // Should be disallowed
return 0;
}

Compilation error received:
---------------------------------------
heap.cpp: In function 'int main()':
heap.cpp:9: error: 'Temp::Temp()' is private
heap.cpp:22: error: within this context
heap.cpp:13: error: 'Temp::~Temp()' is private
heap.cpp:23: error: within this context

Please help me in instantiating the Temp object only in heap, not in
stack.

thanks
Sukumar R

Apr 25 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
WittyGuy wrote:
All I want to achieve is restricting the object instantiation in stack
and allowing the application to instantiate the object only in heap
using new operator. How to achieve this?
Look up the pattern called "factory method".
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 25 '07 #2

P: n/a
WittyGuy wrote:
Hi all,
All I want to achieve is restricting the object instantiation in stack
and allowing the application to instantiate the object only in heap
using new operator. How to achieve this?

I tried out with the following code.
Could you please give me the reason for the compilation error for the
following code:

#include <iostream>

class Temp
{
public:
friend void* ::operator new (size_t);
friend void ::operator delete (void* pDel);
private:
Temp ()
{
std::cout << "Temp Ctor" << std::endl;
}
~Temp ()
{
std::cout << "Temp Dtor" << std::endl;
}
};

int
main ()
{
Temp *pT = new Temp;
delete pT;

// Temp tempObj; // Should be disallowed
return 0;
}

Compilation error received:
---------------------------------------
heap.cpp: In function 'int main()':
heap.cpp:9: error: 'Temp::Temp()' is private
heap.cpp:22: error: within this context
heap.cpp:13: error: 'Temp::~Temp()' is private
heap.cpp:23: error: within this context

Please help me in instantiating the Temp object only in heap, not in
stack.

thanks
Sukumar R
Your code is overly complicated, to prevent object creation/destruction
on stack, one just needs to declare a private destructor. Think about
the life time of a stack object. It's created upon entrance of the
function and destroyed before leaving the function. The compiler must
have access to both constructors and destructor. It's inconvenient or
maybe undesirable to declare private constructors but it's always
straightforward to declare a private destructor.
Apr 25 '07 #3

P: n/a
On Apr 25, 7:19 pm, Fei Liu <fei...@aepnetworks.comwrote:
Your code is overly complicated, to prevent object creation/destruction
on stack, one just needs to declare a private destructor. Think about
the life time of a stack object. It's created upon entrance of the
function and destroyed before leaving the function. The compiler must
have access to both constructors and destructor. It's inconvenient or
maybe undesirable to declare private constructors but it's always
straightforward to declare a private destructor.
Fei,
Yeah I agree with you!
Having ctor in public and dtor in private, we can restrict
instantiation of object in stack.
With that we can achieve allowing instantiation of object only in
heap.
But how will you free the memory used in heap, then?

-
Sukumar R

Apr 26 '07 #4

P: n/a
ajk
On 25 Apr 2007 02:58:47 -0700, WittyGuy <wi**********@gmail.com>
wrote:
>Hi all,
All I want to achieve is restricting the object instantiation in stack
and allowing the application to instantiate the object only in heap
using new operator. How to achieve this?

I tried out with the following code.
Could you please give me the reason for the compilation error for the
following code:

#include <iostream>

class Temp
{
public:
friend void* ::operator new (size_t);
friend void ::operator delete (void* pDel);
private:
Temp ()
{
std::cout << "Temp Ctor" << std::endl;
}
~Temp ()
{
std::cout << "Temp Dtor" << std::endl;
}
};

int
main ()
{
Temp *pT = new Temp;
delete pT;

// Temp tempObj; // Should be disallowed
return 0;
}

Compilation error received:
---------------------------------------
heap.cpp: In function 'int main()':
heap.cpp:9: error: 'Temp::Temp()' is private
heap.cpp:22: error: within this context
heap.cpp:13: error: 'Temp::~Temp()' is private
heap.cpp:23: error: within this context

Please help me in instantiating the Temp object only in heap, not in
stack.

thanks
Sukumar R
class Temp {
public:
~Temp() { ; }
auto_ptr<TempgetInstance() { return new Temp; }
private:
Temp() { ; }
};
Apr 26 '07 #5

P: n/a
On Apr 26, 8:22 am, WittyGuy <wittyguys...@gmail.comwrote:
On Apr 25, 7:19 pm, Fei Liu <fei...@aepnetworks.comwrote:
Your code is overly complicated, to prevent object creation/destruction
on stack, one just needs to declare a private destructor. Think about
the life time of a stack object. It's created upon entrance of the
function and destroyed before leaving the function. The compiler must
have access to both constructors and destructor. It's inconvenient or
maybe undesirable to declare private constructors but it's always
straightforward to declare a private destructor.
Having ctor in public and dtor in private, we can restrict
instantiation of object in stack.
With that we can achieve allowing instantiation of object only in
heap.
But how will you free the memory used in heap, then?
By calling a static member function to do the job, e.g.:

class Doh
{
public:
static kill( Doh* p )
{
delete p ;
}

private:
~Doh() ;
} ;

The problem with declaring the destructor private is that you
can't derive from the class.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.