C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

c++ question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Nan Li
Guest





PostPosted: Wed Sep 28, 2005 1:27 pm    Post subject: c++ question Reply with 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
}

Back to top
mlimber
Guest





PostPosted: Wed Sep 28, 2005 1:32 pm    Post subject: Re: c++ question Reply with quote




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





PostPosted: Wed Sep 28, 2005 1:33 pm    Post subject: Re: c++ question Reply with quote



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





PostPosted: Wed Sep 28, 2005 1:38 pm    Post subject: Re: c++ question Reply with quote


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





PostPosted: Wed Sep 28, 2005 3:13 pm    Post subject: Re: c++ question Reply with quote

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





PostPosted: Wed Sep 28, 2005 3:30 pm    Post subject: Re: c++ question Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.