What is wrong with the code

A

arun_shamli

class CDate
{
public:
CDate() {}
CDate(const CDate& date) {}
};
CDate function1()
{
CDate date();
return date;
}

CDate function2()
{
CDate date;
return date;
}

function1 does not compile while function2 works fine. someone please
explain what is wrong in the first line of function1.
 
M

Marcus Kwok

class CDate
{
public:
CDate() {}
CDate(const CDate& date) {}
};
CDate function1()
{
CDate date();
return date;
}

CDate function2()
{
CDate date;
return date;
}

function1 does not compile while function2 works fine. someone please
explain what is wrong in the first line of function1.

In C++, the general rule is, "if it looks like a function declaration,
then it is a function declaration". Therefore, the first line of
function1 looks like a declaration for a function named "date" that
takes no parameters and returns a CDate.
 
V

Victor Bazarov

class CDate
{
public:
CDate() {}
CDate(const CDate& date) {}
};
CDate function1()
{
CDate date();

The line above declares a function. Lose the parentheses.
return date;
}

CDate function2()
{
CDate date;
return date;
}

function1 does not compile while function2 works fine. someone please
explain what is wrong in the first line of function1.

V
 
J

Jonathan Mcdougall

class CDate
{
public:
CDate() {}
CDate(const CDate& date) {}
};
CDate function1()
{
CDate date();

This is the declaration of a function named "date", taking no
arguments and returning a CDate. Compare it with

void f();
return date;
}

CDate function2()
{
CDate date;

This is an object named "date" of type CDate.
return date;
}

function1 does not compile while function2 works fine. someone please
explain what is wrong in the first line of function1.


Jonathan
 
A

arun_shamli

So when you have to create an object with default constructor you
should never use the paranthesis, otherwise the compiler thinks it as a
function call.


for other constructor (other than the default constructor) the compiler
knows it is not a declaration and does the right thing.

is that right?
 
V

Victor Bazarov

So when you have to create an object with default constructor you
should never use the paranthesis, otherwise the compiler thinks it as
a function call.

No, that's not true either.

Type()

is an expression that does *create* a temporary object using the
*default constructor*.
for other constructor (other than the default constructor) the
compiler knows it is not a declaration and does the right thing.

is that right?

It's other way around. If the compiler can interpret a statement
as a declaration rather than something else, it will.

struct Type { Type(int) {} };
char a = 42;
Type t(int(a)); // declaration of a function.
Type tt(a); // declares/defines/initialises an object

It takes time to get used to providing _less_ information than you
think necessary.

V
 
J

Jonathan Mcdougall

So when you have to create an object with default constructor you
should never use the paranthesis, otherwise the compiler thinks it as a
function call.

Not a function call, a function declaration.

// declares a function 't' returning a T and
// taking no arguments
T t();

However, when it is not possible for a construct to be a declaration,
it is not:

// T() creates a temporary object
T t = T();

or

class C
{
public:
C()
: x() // zero-initializes 'x'
{
}

private:
int x;
};

So don't say "never use parentheses for default constructors", say "be
aware of the potential problems with parentheses and default
constructors".
for other constructor (other than the default constructor) the compiler
knows it is not a declaration and does the right thing.

Most of the times yes. However, when it is possible for the construct
to be either a declaration or a definition, it is always a declaration.
Take this:

class Foo{public: void blah(); };
class Bar{};

void f()
{
Foo x(Bar()); // are you sure?
x.blah(); // oups!
}

"ComeauTest.c", line 7: error: expression must have class type
x.blah();
^

See http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.19 for more
informations.


Jonathan
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top