473,795 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how can we restrict object creation on heap ?

how can we restrict object creation on heap ?

soln1: overload new

any other way to do this ?

Dec 22 '06 #1
4 3306

shaanxxx wrote:
how can we restrict object creation on heap ?

soln1: overload new

any other way to do this ?
The only way I can think is to create your own memory pool and check
the address of the object is in it in the constructor. OTOH maybe you
can get at the heap top and bottom pointers for a similar result. Not
sure how to do that portably though.

regards
Andy Little

Dec 22 '06 #2
shaanxxx wrote:
how can we restrict object creation on heap ?
I think you mean require class instances to be in heap.
>
soln1: overload new
Wrong, unless I misunderstood above.
>
any other way to do this ?
Easy, portable way: make all constructors
private, then provide a static member that
creates an instance on the stack, and
returns a pointer, like X::make_inst for
example:

static X * make_inst() { return(new X); }

Dec 22 '06 #3

wk****@yahoo.co m wrote:
shaanxxx wrote:
how can we restrict object creation on heap ?

I think you mean require class instances to be in heap.

soln1: overload new

Wrong, unless I misunderstood above.

any other way to do this ?

Easy, portable way: make all constructors
private, then provide a static member that
creates an instance on the stack, and
returns a pointer, like X::make_inst for
example:

static X * make_inst() { return(new X); }
You pay quite a high price just to ensure construction only on the
heap...

class X{
private:
X(){}
friend X* make_X(){return new X();}
protected:
virtual void f(){};
};

class Y : protected X{
public:
Y() : X(){}
void f(){}
};

int main()
{
Y y;
}
regards
Andy Little

Dec 22 '06 #4
wk****@yahoo.co m wrote:
shaanxxx wrote:
how can we restrict object creation on heap ?

I think you mean require class instances to be in heap.

soln1: overload new

Wrong, unless I misunderstood above.

any other way to do this ?

Easy, portable way: make all constructors
private, then provide a static member that
creates an instance on the stack, and
returns a pointer, like X::make_inst for
example:

static X * make_inst() { return(new X); }
An alternative:

If the objective is to *solely* limit object creation to use the heap,
it may be easier at times to declare the destructor as private instead
of the constructor. If the class has many constructors, you must
remember to make all the constructors private. This also includes both
the copy and default constructors if they would otherwise be generated
by the compiler.

If you just declare the destructor private, all you need is to declare
one method private, and provide a psuedo-destructor that simply deletes
the object...

Dec 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2676
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory location(s) pointed to, so that only one declared pointer can store that address and access the data in those memory blocks, where I the data in those location(s) can be changed. Is that a correct understanding?
1
1765
by: Jean Stax | last post by:
Hi ! A couple of pretty basic questions: Value types: As far as I understand, when I create value type without "new" syntax the object is considered as unutilized. Consequently, I have to initialize its member variables manually; otherwise I would get an exception while accessing them.
3
1389
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following way. In the constructor i have created the connection instant in the following way
7
1997
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() { m_RsProxy = new ReportingServiceProxy();
12
2503
by: Me | last post by:
I'm trying to wrap my head around the wording but from what I think the standard says: 1. it's impossible to swap a restrict pointer with another pointer, i.e. int a = 1, b = 2; int * restrict ap = &a; int * restrict bp = &b;
21
6530
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no definition found in K&R 2nd.
7
2862
by: Arpan | last post by:
The .NET Framework 2.0 documentation states that An Object variable always holds a pointer to the data, never the data itself. Now w.r.t. the following ASP.NET code snippet, can someone please explain me what does the above statement mean? <script runat="server"> Class Clock
2
1472
by: Anane | last post by:
When we use NEW word to create object of a class, it always creates object on HEAP memory. How can one create object on stack? If one creates object inside a function, then also object will be created in HEAP. Only object reference will be in stack. When function ends, object reference is destroyed, eventually object will be destroyed by GC. Am I correct with this concept?
0
2990
by: copx | last post by:
Restrict keyword questions How far does the guarantee that an object is not accessed through another pointer go? I mean, all examples I have seen are simple stuff like: int f (int *restrict x, int *restrict y) { *x = 0; *y = 1; return *x;
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.