 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nan Li Guest
|
Posted: Wed Sep 28, 2005 1:27 pm Post subject: c++ question |
|
|
Hello, all,
I have the following code. I don't know what 'A a();' means in the
main function, though it can be compiled.
Thanks a lot,
Nan
class A
{
public:
int i;
explicit A(int i) {}
};
int main()
{
A b(1);
b.i = 2;
// A a(); //compile OK. But what does this mean?
// a.i = 1; //compile failure
}
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Wed Sep 28, 2005 1:32 pm Post subject: Re: c++ question |
|
|
Nan Li wrote:
| Quote: | Hello, all,
I have the following code. I don't know what 'A a();' means in the
main function, though it can be compiled.
Thanks a lot,
Nan
class A
{
public:
int i;
explicit A(int i) {}
};
int main()
{
A b(1);
b.i = 2;
// A a(); //compile OK. But what does this mean?
|
This line declares a function that returns an object of type A. If you
had a default constructor in A, then it would create an object of type
A and the following line would compile also. As it stands, however, the
function a is not a class and has no members.
| Quote: | // a.i = 1; //compile failure
}
|
Cheers! --M
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Wed Sep 28, 2005 1:33 pm Post subject: Re: c++ question |
|
|
Nan Li wrote:
| Quote: |
Hello, all,
I have the following code. I don't know what 'A a();' means in the
main function, though it can be compiled.
Thanks a lot,
Nan
class A
{
public:
int i;
explicit A(int i) {}
};
int main()
{
A b(1);
b.i = 2;
// A a(); //compile OK. But what does this mean?
// a.i = 1; //compile failure
}
|
It is a function declaration ( In C-speak: a prototype)
It specifies, that somewhere there is a function called 'a' which
does not take any arguments and returns an A object.
What you want, is:
A a;
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Wed Sep 28, 2005 1:38 pm Post subject: Re: c++ question |
|
|
Karl Heinz Buchegger wrote:
| Quote: | Nan Li wrote:
Hello, all,
I have the following code. I don't know what 'A a();' means in the
main function, though it can be compiled.
Thanks a lot,
Nan
class A
{
public:
int i;
explicit A(int i) {}
};
int main()
{
A b(1);
b.i = 2;
// A a(); //compile OK. But what does this mean?
// a.i = 1; //compile failure
}
It is a function declaration ( In C-speak: a prototype)
It specifies, that somewhere there is a function called 'a' which
does not take any arguments and returns an A object.
What you want, is:
A a;
|
That won't work either without a default constructor in A. You could
write:
A a( 2 );
Cheers! --M
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Wed Sep 28, 2005 3:13 pm Post subject: Re: c++ question |
|
|
mlimber wrote:
| Quote: |
What you want, is:
A a;
That won't work either without a default constructor in A.
|
Yep. I missed that.
Thanks for the correction.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Nan Li Guest
|
Posted: Wed Sep 28, 2005 3:30 pm Post subject: Re: c++ question |
|
|
Thank you for your quick replies. I rewrote the code as below
#include <iostream>
class A
{
public:
int i;
explicit A(int i) {}
};
int main()
{
A b(1);
b.i = 2;
A a();
a();
}
A a(){
A a(1);
std::cout << "here" << std::endl;
return a;
}
results:
[nan@xxx test]$ g++ test1.cpp && ./a.out
here
If I don't have 'A a()' declared in the main function , g++ gives
error `A a()' used prior to declaration' , as expected.
Thanks again
Nan
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|