473,732 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Smart Pointers and "new"

Hello All,

I just learnt about smart pointers. Given the advantages of using
smart-pointers, as a designer of say a List object, I can inhibit
creation of the actual objects by making its constructors and then
making the smart pointer a friend of the class (I am talking about a
specific smart pointer implementation for a specific object). Now, it
seems to me that inhibiting the use of "new" with the smart pointer
would enhance things. For example, even if I created a smart pointer
and restricted all access to via the pointer, the user can still
create smart pointer objects themselves with new right? So, unless the
user deletes these explicitly, there is still a chance of leaks right?
I am sure others must have thought about it and so must have the
language designers. My question is, is it possible to inhibit dynamic
object creation via "new" for specific classes?

thanx,
vijai.
Jul 22 '05 #1
2 1793
* vi************* *@comcast.net (Vijayaraghavan Kalyanapasupath y) schriebt:

is it possible to inhibit dynamic object creation via "new" for
specific classes?


Yes, by making inaccessible something needed by "new" (constructor,
destructor, full definition of class).

Usually that means some factory pattern or a singleton.

The downside is that it tends to diminish the usefulness of the class,
and is much work for little gain.

Jul 22 '05 #2

"Vijayaragh avan Kalyanapasupath y" <vi************ **@comcast.net> wrote in
message news:97******** *************** **@posting.goog le.com...
Hello All,

I just learnt about smart pointers. Given the advantages of using
smart-pointers, as a designer of say a List object, I can inhibit
creation of the actual objects by making its constructors and then
making the smart pointer a friend of the class (I am talking about a
specific smart pointer implementation for a specific object). Now, it
seems to me that inhibiting the use of "new" with the smart pointer
would enhance things. For example, even if I created a smart pointer
and restricted all access to via the pointer, the user can still
create smart pointer objects themselves with new right? So, unless the
user deletes these explicitly, there is still a chance of leaks right?
I am sure others must have thought about it and so must have the
language designers. My question is, is it possible to inhibit dynamic
object creation via "new" for specific classes?


See www.boost.org

class A : boost::noncopya ble
{
int m1;

A();
A( int aArg1 ):m1(aArg1){}
public:
typedef boost::shared_p tr<A> tPtr;

static tPtr Make( int aArg1 ){ return tPtr( new A( aArg1 ) ); }

...
};

int main()
{
A::tPtr lAPtr = A::Make( 123 );

A* lRawAPtr = new A( 456 ); // fails
}

Jeff F
Jul 22 '05 #3

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

Similar topics

16
2122
by: garyolsen | last post by:
For a class, MyClass, there are two ways to instantiate an object: 1. MyClass *MC = new MyClass(); 2. MyClass MC; In general, when should you have to use 1 and when 2? What're advantages/disadvantages of using either? Thanks!
30
3572
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there no other choice but using "new")
24
2866
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have a class called polynomial. Its a nothing little class right now, with just int variables, a basic container class. Im using it as I go through some tutorials, but in this particular tutorial its telling me to do polynomial *first = new...
15
2293
by: b83503104 | last post by:
Hi, class MyClass{ int array_size; HisClass **hisObject; }; I want hisObject to point to an array of HisClass objects. The size of the array is given by array_size.
2
1250
by: PengYu.UT | last post by:
Hi, I saw the following code: Composition *quick = new Composition(new SimpleCompositor); SimpleCompositor and Composition are both classes. My question is how to delete the newed SimpleCompositor object.
51
3950
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and output to be within one directory, and I want to be able to combine files from different directories into another directory of my choice). My program seems to work fine, but I'm wondering about this loop: for (int i = 0; i < numFiles; i++)
36
2585
by: Pat | last post by:
Hi, I've run into a strange problem, but one that seems like it might be fairly common. I have a single base class, from which several other classes are derived. To keep the example simple, the base class is "Animal" and the derived classes are "Cat," "Dog," and "Horse." The base class has a pure virtual method:
350
11811
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
30
3834
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at the end of the app for example: class test { public: int x;
0
8946
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
8774
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
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
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
9235
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
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.