Difference between these two declaration of class t1

M

Manoj

What is the difference between these two declaration of class t1

class t1
{
t1()
{
cout << "Default Constructor" ;
}
}

main()
{
t1 t;
t1 t();
}
 
J

James Kanze

What is the difference between these two declaration of class t1

I only see one "declaration" of class t1 in the program, and
it's a definition. Perhaps you meant declarations of t (rather
than t1).
class t1
{
t1()
{
cout << "Default Constructor" ;
}
}
main()
{
t1 t;

Defines a local variable (in technical terms: no linkage,
automatic lifetime, block scope) of type t1, named t.

Declares (but does not define) a global function (in technical
terms, external linkage and namespace scope---lifetime isn't
relevant for functions).

This is usually *not* what you want.
 
T

Tomasz Krol

Manoj said:
What is the difference between these two declaration of class t1

class t1
{
t1()
{
cout << "Default Constructor" ;
}
}

main()
{
t1 t;
t1 t();
}


t1 t - definition of object t1, in this case default constructor will be
called

t1 t() - declaration of function t, constructor won't be called
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top