Ask question, find answer on any topic in real time from people around the world. Have a question ? Ask now. Know an Answer share your Knowledge to world.PrepJunk is the online community that has Junk of answers!
FINLAND
21
UNITED STATES
1
CHINA
1
UNKNOWN
1
24
online

What is a constructor? Why would I ever use one?declared?

0

What is a constructor? Why would I ever use one?declared?

asked Apr 4, 2012 in C++ by anonymous
    

1 Answer

0

Objects should establish and maintain their own internal coherence. The �maintaining' part is done by ensuring self-consistency is restored after any operation completes (ex: by incrementing the link count after adding a new link to a linked list). The part about �establishing coherence' is the job of a constructor.
Constructors are like �init functions'; they build a valid object. The constructor turns a pile of incoherent arbitrary bits into a living object. Minimally it initializes any internally used fields that are needed, but it may also allocate resources (memory, files, semaphores, sockets, ...).

A constructor is like a �factory': it builds objects from dust.

�ctor' is a typical abbreviation for constructor.

answered Apr 4, 2012 by anonymous