You are here:

C++/question of c++


1.Why constructor don't return their address?
2.Why we always define constructor in public domain?
3.Why constructor are not virtual?

1/ Why should they? Constructors initialise an object. The object already has a memory location. Also why would an address be needed in the following?

   MyClass object(1,2,3);

Storage allocation and object initialisation (construction) are separate activities, just as object destruction and storage deallocation are separate activities.

2/ No you do not. Consider the following idiomatic C++:

   class NotCopyable
   // publically available operations

   // Declare but do not define copy constructor
   // and assignment operator
       NotCopyable(NotCopyable const &);
       NotCopyable & operator=( NotCopyable const &);

Declaring and not defining a copy constructor and assignment operator prevents anyone calling them and therefore prevents anyone copying or assigning to objects of such a class. However if left public this would only be picked up at link time when the linker failed to find function definitions for them. Making them private forces such uses to be detected by the compiler, which usually means a much more meaningful error - usually with file and line number information of the offending code.

Other non-public uses of constructors would be to make some (or all) of them protected so that only classes derived from such a class can make use of them.

3/ And how exactly would this work? You make virtual (polymorphic, dynamically determined) calls through a pointer or reference to a base class. How then would you do this for a constructor? At this time you _must_ give the exact type of the object you are creating. Hence there can be no useful polymorphism at this point. In fact C++ gives no guarantees about calling virtual functions during construction and destruction. This is because at this time the virtual function mechanisms are beings setup (construction) and torn down (destruction). Calling a virtual function from a base class constructor will call the base class version of the function not a derived override. This is because objects are initialised from the base down, so effectively when the base is being built the derived part of the object does not yet exist (although raw memory for it will exist). The same is true of destruction but in reverse: an object is destroyed from the derived towards the base thus when a base destructor is called the derived parts of the object can be considered as destroyed already.

You can however use a virtual function - maybe called something like Create - to create a specific object type from a base class pointer or reference. The idea is that all derived classes override this virtual create and return a new object of their type:

   class Base
       virtual Base * Create();
       virtual ~Base() {}

   // ...


   // ...

   class Derived : public Base
       Derived * Create();

   // ...


   // ...

   Base * Base::Create()
       return new Base;

   // ...

   Derived * Derived::Create()
       return new Derived;

Note that the difference in the return types is permitted as Derived is derived from Base - this is a feature called covariant return types. However some compilers may not support it in which case just changed the Derived::Create's return type to a Base *.

Hope this helps a bit...  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.