Showing posts with label Explicit. Show all posts
Showing posts with label Explicit. Show all posts

Sunday, May 9, 2010

Auotmatic Type Conversion and use of Explicit Keyword

1. By Constructor Conversion :
        See this situation .

Output :
Constructor B called
Hi inside Function
--------------------------------------------------------------------------------
Preventing constructor conversion.
This Behaviour of automatic calling of constructor . can be stopped if constructor is explicit.
Explicit Constructor: Constructor with explicit keyword.It prevents compliler from performing implicit (Unexpected) type conversions.


After this   func (a); will not get compiled .

Output:Compliation Fails
auotTypeConversion.cpp: In function `int main()':
auotTypeConversion.cpp:18: error: conversion from `A' to non-scalar type `B' requested

2. Operator Conversion :

OUTPUT:
inside conversion operator B--> A
inside A constructor i=10
Function call

Followers