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

Types of constructors

What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.

Jul 23 '05 #1
4 4103
Sathyaish wrote:
What is a private constructor,
It's a constructor that is declared in a private section of a class.
and why would a class have one?
Because it might not want other classes to be able to create instances of
it.
What are the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.


Just like any member, a constructor can be public, protected or private. And
just like normal member functions it can have parameters or not.

There is a special kind of constructors, the conversion constructors. A
constructor that can be called with exactly one argument is considered a
conversion constructor, used to convert the parameter type into the class
the constructor belongs to.
And there is a special kind of conversion constructors, the copy
constructor. It is used to create a copy of an existing object of the same
class. Even though both classes are the same, it counts as a conversion
constructor.
Jul 23 '05 #2
That is a treasure, Rolf. Thank you so very much.

I understood every bit of it. I want some more. Can you please provide
me some simple, easy-to-understand code snippets for conversion
constructors and copy constructors?

Jul 23 '05 #3

"Sathyaish" <Sa*******@Yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
That is a treasure, Rolf. Thank you so very much.

I understood every bit of it. I want some more. Can you please provide
me some simple, easy-to-understand code snippets for conversion
constructors and copy constructors?


class A
{
public:
/* 'conversion constructor': */
/* converts a type 'int' object */
/* to a type 'A' object */
A(int)
{
}

/* 'copy constructor': */
/* creates a copy of an existing */
/* type 'A' object */
A(const A& a)
{
}
};

int main()
{
int i(0);

A a1(i); /* creates type 'A' object by */
/* converting from type 'int' object 'i' */
/* (calls constructor 'A::A(int) )' */

A a2(a1); /* creates a copy of type 'A' object 'a1' */
/* (calls constructor 'A::A(const A&) )' */

return 0;
}

-Mike
Jul 23 '05 #4
Thanks very much, Mike. I understood it a lot better with that little
snippet.

Jul 23 '05 #5

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
3
by: Cliff Harris | last post by:
I have an odd question that I'm hoping someone can help with. I simply (or not simply?) need to enumerate through all of the data types in the .Net framework. I do understand that if this is...
5
by: PaulW | last post by:
Personally, I like to use simple abstractions to wrap resources For example consider a class that wraps a resource - say an IntPtr which is allocated in the constructor and deallocated in the...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
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?
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
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...
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.