cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : General C++ Programming : CONSTRUCTOR(OBJECT)
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  CONSTRUCTOR(OBJECT)

G SATISH KUMAR (16)
A CONSTRUCTOR IS A MEMBER FUNCTION THEN WHY DO U THINK OBJECTS CANNOT BE USED AS PARAMETERS.WE USE CONSTRUCTOR_NAME(OBJECT &); IN COPY CONSTUCTOR BUT NOT
CONSTRUCTOR_NAME(OBJECT);
WHY CONSTRUCTOR ALTHOUH A MEMBER FUNCTION CANNOT TAKE OBJECT AS PARAMETER, IT TAKES OBJECT PASSED BY REFERENCE AS PARAMETER AS IN COPY CONSTRUCTOR.
|
msram (19)
Because if it is not by reference, it has to be either
1. a call by value
or
2. a call by address (through pointers).

Each of these methods has a problem.

In the first case (where the object is passed BY-VALUE), the formal parameter needs to be created with the exact state as that of the actual parameter object which leads to the invocation of the copy constructor recursively infinitely.

In the second case (where we pass a pointer), there is a chance that a NULL pointer or an uninitialized pointer be passed as a parameter in which case the application will crash.

To avoid these two problems, reference variables are the best solution. First problem is obviously resolved by using references. And coming to the second problem... when we have a reference variable, if we pass an initialized pointer, the compiler itself will show an error; so there would not be a chance of runtime error.
| Last edited on

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us