On Mon, Oct 24, 2011 at 07:09:51PM +0530, aravind vijayan wrote:
> HI,
> Let me post a replay for a small discussion on last meeting:
>
> # include <iostream>
>
> using std::cout;
>
> class abc
> {
> public:
> abc(){cout<<"In abc";}
> };
> class xyz
> {
> public:
> xyz(abc x)
> {cout<<"In xyz";}
> void show(){cout<<"In show";}
> };
> int main()
> {
> xyz obj(abc());
> obj.show();
> return 0;
> }
>
>
> here we declaring a function via:xyz obj(abc());
>
> you can find it your self by using sizeof operator...
>
> to make obj an object you will have to add ()
>
> as:
>
> xyz obj((abc()));
>
> it will treated as object creation...
>
Aravind,
The answer to this issue is that the statement
xyz obj(abc());
is not an object declaration. It is a function declaration, which is inherited
from the C
Language. In this particular statement, it happens because the constructor
abc() doesn't have a
return value. So it is parsed by the C++ compiler as a function declaration
with parameter type
abc(*)(). Thus above statement will neither call the constructor abc() nor
create an object obj as
you expected. obj.show() will show an error as obj is not an object and it is a
function name.
The following modified statements are parsed by the C++ compiler as object
declarations, as
they cannot be function declarations. So it calls the constructor abc(),
explicitly.
xyz obj((abc()));
xyz obj(1, abc());
xyz obj(abc(), 1);
xyz obj(abc(1));
This type of constructor call is valid, as it is used to create a temporary
object within a function.
I hope it is clear.
Happy Hacking,
Jos Collin
_______________________________________________
Indian Libre User Group Cochin Mailing List
http://www.ilug-cochin.org/mailing-list/
http://mail.ilug-cochin.org/mailman/listinfo/mailinglist_ilug-cochin.org
#[email protected]