Difference Between List x; and List x(); , if 'List' is a Class?

R

roopa

I have declared a class with name List;
and in main() function, i have declared the List object as follows


class List
{
public:
List()
{
cout<<"In List Constuctor";
}
};


int main(int argc, char* argv[])
{
List x1;
List x2();
}

Could anybody let me know, What is the difference between "List x1;"
and "List x2();" ?

Thanks in advance.
 
S

Sharad Kala

roopa said:
I have declared a class with name List;
and in main() function, i have declared the List object as follows


class List
{
public:
List()
{
cout<<"In List Constuctor";
}
};


int main(int argc, char* argv[])
{
List x1;

This is an object of class List.
List x2();

This declares a function x2 which takes no parameters and returns a List
object. The rule in C++ is that whatsoever can be parsed as a function
declarartion is done so.

-Sharad
 
A

Alex Vinokur

roopa said:
I have declared a class with name List;
and in main() function, i have declared the List object as follows


class List
{
public:
List()
{
cout<<"In List Constuctor";
}
};


int main(int argc, char* argv[])
{
List x1;
List x2();
}

Could anybody let me know, What is the difference between "List x1;"
and "List x2();" ?

Thanks in advance.

See relevant discussion at
http://groups.google.com/[email protected]
 
M

Mike Wahler

Re: Difference Between List x; and List x(); , if 'List' is a Class?

For this issue it makes no difference what the type of 'List' is.
I have declared a class with name List;
and in main() function, i have declared the List object as follows


class List
{
public:
List()
{
cout<<"In List Constuctor";
}
};


int main(int argc, char* argv[])
{
List x1;
List x2();
}

Could anybody let me know, What is the difference between "List x1;"
and "List x2();" ?

List x1;
/* creates object of type 'List', named 'x1'. */

List x2();
/* Declares (but doesn't define) a function nameed 'x2',
that takes no arguments and returns a value of type 'List'. */

You should get a 'missing function' error at link time.

-Mike
 
S

Stephen Howe

You should get a 'missing function' error at link time.

No you won't.

Stephen Howe
 
A

Andrey Tarasevich

Mike said:
List x1;
/* creates object of type 'List', named 'x1'. */

List x2();
/* Declares (but doesn't define) a function nameed 'x2',
that takes no arguments and returns a value of type 'List'. */

You should get a 'missing function' error at link time.
...

Only if you actually try to call the function.
 
J

Jerry Coffin

(e-mail address removed) (roopa) wrote in message
[ ... ]
Could anybody let me know, What is the difference between "List x1;"
and "List x2();" ?

A better question would be: "is there any real similiarity between the
two", to which the answer would be a barely qualified "No."

"List x1;" defines a List object named x1. "List x2();" declares a
function named x2 that takes no parameters and returns a List.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top