By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

Types of constructors

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
Thanks very much, Mike. I understood it a lot better with that little
snippet.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.