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

W

WittyGuy

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* ::eek:perator new (size_t);
friend void ::eek:perator 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
 
V

Victor Bazarov

WittyGuy said:
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
 
F

Fei Liu

WittyGuy said:
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* ::eek:perator new (size_t);
friend void ::eek:perator 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.
 
W

WittyGuy

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
 
A

ajk

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* ::eek:perator new (size_t);
friend void ::eek:perator 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<Temp> getInstance() { return new Temp; }
private:
Temp() { ; }
};
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top