473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What is wrong with the code

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.

May 24 '06 #1
7 6264
ar*********@yahoo.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 24 '06 #2
arun_sha...@yahoo.com wrote:
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

May 24 '06 #3
ar*********@yahoo.com wrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 24 '06 #4
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?

May 24 '06 #5
ar*********@yahoo.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 24 '06 #6
arun_sha...@yahoo.com wrote:
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-lit...html#faq-10.19 for more
informations.
Jonathan

May 24 '06 #7
ar*********@yahoo.com wrote:
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.


(snip)

Please read http://cfaj.freeshell.org/google/.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
May 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
56
by: Cherrish Vaidiyan | last post by:
Frinds, Hope everyone is doing fine.i feel pointers to be the most toughest part in C. i have just completed learning pointers & arrays related portions. I need to attend technical interview on...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.