473,394 Members | 1,724 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,394 software developers and data experts.

can constructor be private or not

sanjay123456
125 100+
Dear friends,
can constructor be private or not if yes How it possible ?

sanjay
Dec 7 '06 #1
3 1561
Ganon11
3,652 Expert 2GB
I don't think a constructor can be private - or if it can, I don't think it should. Whenever you create an instance of a class, you have to call SOME constructor. Since whatever class you are using to define this object is not of the same type, it shouldn't be able to access the private constructor.
Dec 7 '06 #2
Dear friends,
can constructor be private or not if yes How it possible ?

sanjay

hi there,

Constructors cannot be called explicitly as if they were regular member functions. They are only executed when a new object of that class is created.
You can also see how neither the prototype nor the later constructor declaration includes a return value; not even void.

Like any other function, a constructor can also be overloaded with more than one function that have the same name but different types or number of parameters. Remember that for overloaded functions the compiler will call the one whose parameters match the arguments used in the function call. In the case of constructors, which are automatically called when an object is created, the one executed is the one that matches the arguments passed on the object declaration:

If you do not declare any constructors in a class definition, the compiler assumes the class to have a default constructor with no arguments.

so it is better to put then in public not on private..

regards,
Dec 8 '06 #3
DeMan
1,806 1GB
More to the point, a constructor is used to create an instance of a class. For a class (other than a static class, which doesn't (always) need to be initialised) it doesn't make sense for a constructor to be private.

That is constructors are the crux of Object Oriented Programming, and the point of OOP is that you create objects which can do things, rather than just trying to do things on objects....to create an object, you need to be able to see the method to create it (the constructor).

It is probably possible to create objects without using the constructor (in some situations), but I don't think it makes (too much) sense for a constructor to be limited only to the class which it creates an instance of.

Certainly even if such cases exist they would be unlikely to come up in everyday situations.
Dec 8 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
11
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
15
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new...
6
by: Henry | last post by:
I was trying to derive a class from System.Windows.Forms.ComboBox. My goal was to create a class that loaded its own data. I did not want to create too many objects, so I tried to share a Database...
19
by: Andrew J. Marshall | last post by:
I want to create a class that must receive a parameter when instantiated. In other words, I do not want it to have a "Public Sub New()". 1) Does VB.NET create a default public constructor if I do...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
2
by: tshad | last post by:
Using VB.Net VS 2003 I am getting the following message when I call my 2nd constructor. Too many arguments to 'Public Sub New()'. I have 2 constructors: **********************************...
3
by: Rahul | last post by:
Hi Everyone, I have the following code and the compiler complains that there isn't any default constructor available, class C { private: C() {
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
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...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.