473,385 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1778
* vi**************@comcast.net (Vijayaraghavan Kalyanapasupathy) 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

"Vijayaraghavan Kalyanapasupathy" <vi**************@comcast.net> wrote in
message news:97*************************@posting.google.co m...
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::noncopyable
{
int m1;

A();
A( int aArg1 ):m1(aArg1){}
public:
typedef boost::shared_ptr<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
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...
30
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...
24
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...
15
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
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...
51
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...
36
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,...
350
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...
30
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.