what is the difference is creating object

hi all,

Class A
{
public:
A(){cout<<" A() "<<endl; }
};

A Foo () // why it is not giving compilation error since return type is A and I'm not mention return
{
}
int main ()
{
A a(); // why constructor or destructor is not geting called??
cout<<"End of main "<<endl;
}


can any one explain me what is happening ???

Regards
Vijay
1
2
3
A Foo () // why it is not giving compilation error since return type is A and I'm not mention return 
{
}
Either the compiler has downs or the compiler doesnt mind because youre not calling the function anywhere

A a(); // why constructor or destructor is not geting called??
It should be getting called, what compiler/IDE are you using?

Edit:

It might be because your class contains no variables(i have no idea really but..), how about adding like an int to it and try again?
Last edited on
I'm using g++ compiler...

in main ()
{
Foo ();
};

then the output I'm getting is
"A()"
End of Main

what is happening???????? :(
Post the whole code please ~.~
class A
{
int a;
public:
A() { cout<<" A()"<<endl;
}
~A() { cout<<"~A()"<<endl;
};

int main ()
{
A a(); // Nothing got printed....

}
I tried below code

#include <iostream>
using namespace std;

class A
{
public:
A(){cout<<" A() "<<endl; }
};

A Foo () // why it is not giving compilation error since return type is A and I'm not mention return
{
}
int main ()
{
// A a(); // why constructor or destructor is not geting called??
Foo ();
cout<<"End of main "<<endl;
return 0;
}


Behavior seems to be very much compiler specific.

When I tried on VC++ 6 compiler, i got below error

"E:\CPP\test4\test4.cpp(16) : error C4716: 'Foo' : must return a value"

When i tried with Sun CC compiler, i got below error

""test.cpp", line 12: Error: "Foo()" is expected to return a value."

When i tried with Sun g++ compiler, it compiled fine. At runtime, It is printing only below message "End of main " only.

A a();

This defines a prototype for a function called a that returns an A.
Function declarations are allowed at local scope but the definitions.

Below code works fine as expected.

#include <iostream>
using namespace std;

int main ()
{
int p();



cout<<"End of main "<<p()<<endl;

return 0;
}

int p()
{
return 8;
}
Topic archived. No new replies allowed.