473,327 Members | 2,007 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,327 software developers and data experts.

prevent creation of object for a class

Hello!

If I want to prevent creation of object for a class.
I can accomplish this by using different technique.

One way of doing this is to define the class to be abstract even if all the
methods and property is concrete.
A second way is to define the constructor as private.

Is it possible to say which of these is better in some way or is it just a
matter of taste.

//Tony
Aug 25 '06 #1
4 2808

tony wrote:
Hello!

If I want to prevent creation of object for a class.
I can accomplish this by using different technique.

One way of doing this is to define the class to be abstract even if all the
methods and property is concrete.
A second way is to define the constructor as private.

Is it possible to say which of these is better in some way or is it just a
matter of taste.

//Tony

The "static" modifier for a class is a new feature of C# 2.0.

If you are working in 1.1 IMHO I'd recommend declaring the constructor
private like if you were using the singleton pattern.

I find abstract classes more suitable for inheritance so they don't fit
natural in this scenario for me, furthermore you'd likely declare it
sealed.

Aug 25 '06 #2
Hi,

I prefer the second, declaring a class as abstract can give the wrong
impression that you want to force the deriving of it.

Note that in order of this class to be usable you have to define either a
method that returns an instance ( using the private constructor) or if you
want to completely prevent any instance declare all the members as static.

In 2.0 you can declare a class as static thus preventing the creation of any
instance.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"tony" <jo*****************@telia.comwrote in message
news:uQ**************@TK2MSFTNGP02.phx.gbl...
Hello!

If I want to prevent creation of object for a class.
I can accomplish this by using different technique.

One way of doing this is to define the class to be abstract even if all
the
methods and property is concrete.
A second way is to define the constructor as private.

Is it possible to say which of these is better in some way or is it just a
matter of taste.

//Tony


Aug 25 '06 #3
Hello,

I haven't tested this yet but I think that it is impossible to call any
method of an abstract class because it is treaten by the compiler kind
of an interface from that you must derive. So you should use the static
keyword for the class.

Bye

Matthias
tony schrieb:
Hello!

If I want to prevent creation of object for a class.
I can accomplish this by using different technique.

One way of doing this is to define the class to be abstract even if all the
methods and property is concrete.
A second way is to define the constructor as private.

Is it possible to say which of these is better in some way or is it just a
matter of taste.

//Tony

Aug 25 '06 #4

Matthias Heise wrote:
Hello,

I haven't tested this yet but I think that it is impossible to call any
method of an abstract class because it is treaten by the compiler kind
of an interface from that you must derive. So you should use the static
keyword for the class.
In the OP's case he would make the methods static, so he would not have
to create an instance in order to call them (he wants to prevent
instance creation anyway). Static methods on abstract classes are
callable.

Aug 26 '06 #5

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

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
3
by: Joerg Toellner | last post by:
Dear Group, i have a self written c++-class that is used in many projects from many programmers. The functionality of this class requests, for some reasons, that any instance (object) of this...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
0
by: Smokey Grindle | last post by:
I am using the user creation wizard in asp.net 2.0 and using it to send the user an email message when their account is created, well this works great if the address is a valid mailbox, but when it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.