Hello,
If I have a statement
A a(B());
where A and B are both types, then it seems I can have two
interpretations:
1. B() is a casting operation and returns an object of type B, which
is then passed to A's constructor (taking a B object as argument) to
construct an A object. Hence the statement is a definition, defining
an object "a" of type A.
2. B() is the same as B(*)(), hence the statement is a function
declaration: "a" is a function, which takes a pointer to a function
(the function takes no arg and returns a result of type B) and returns
an object of type A.
Both seem reasonable, but which one of them is correct?
Similarly, a statement
A a(B (c));
also seems to have two interpretations, since I can either interpret
"B (c)" as a casting operation, or a pointer to a function.
I vaguely remember that the Standard says if a statement can be
interpreted as a declaration, then it is a declaration. If so, I
guess the statements above are declarations.
Thanks a lot,
Jess 4 1060
Jess wrote:
If I have a statement
[...]
I vaguely remember that the Standard says if a statement can be
interpreted as a declaration, then it is a declaration. If so, I
guess the statements above are declarations.
Yes, they are. You remember just the right thing.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Aug 22, 11:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jess wrote:
If I have a statement
[...]
I vaguely remember that the Standard says if a statement can be
interpreted as a declaration, then it is a declaration. If so, I
guess the statements above are declarations.
Yes, they are. You remember just the right thing.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks. If I'd like to define a shared_ptr object using
std::tr1::shared_ptr<ApA(createA());
where createA() returns a A*, then is this still a declaration?
Again, it can be interpreted as a declaration and a definition, but
clearly, I'd like to define pA as a shared_ptr<Aobject.
Thanks,
Jess
Jess wrote:
[..] If I'd like to define a shared_ptr object using
std::tr1::shared_ptr<ApA(createA());
where createA() returns a A*, then is this still a declaration?
If 'createA' is a function (and not a type), then it cannot be
interpreted as a declaration.
Again, it can be interpreted as a declaration and a definition, but
clearly, I'd like to define pA as a shared_ptr<Aobject.
If 'createA' is a type, and you intend to use a temporary of that
type to do what you need, then you shouldn't use the parenthesised
form, but instead use the assignment form:
... pA = createA();
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Aug 26, 12:18 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jess wrote:
[..] If I'd like to define a shared_ptr object using
std::tr1::shared_ptr<ApA(createA());
where createA() returns a A*, then is this still a declaration?
If 'createA' is a function (and not a type), then it cannot be
interpreted as a declaration.
Again, it can be interpreted as a declaration and a definition, but
clearly, I'd like to define pA as a shared_ptr<Aobject.
If 'createA' is a type, and you intend to use a temporary of that
type to do what you need, then you shouldn't use the parenthesised
form, but instead use the assignment form:
... pA = createA();
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for the tips. :)
Jess This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven T. Hatton |
last post by:
Now why didn't *_I_* think of that!
Declarations and Definitions in One Header
C++ object definitions can be quite complex. In principle, your source code
will need two kinds of things for...
|
by: Chris Gordon-Smith |
last post by:
I am currently in India and have treated myself to the Indian reprint
of O'Reilly's "C++ In A Nutshell". (Books in India come in at 1/3 to
1/2 of the price in Britain.)
I thought that I would...
|
by: Arthur J. O'Dwyer |
last post by:
Well, I'm trying to write that program that was requested
a few weeks back, the one that could take struct definitions
and create portable functions to read and write those
structs. Hence the...
|
by: Neil Zanella |
last post by:
Hello,
Unlike in pre-C99 versions of C where variables can only be defined at the
beginning of blocks, C99 allows variables to be defined in arbitrary
places inside blocks. However, gcc 3.2.2...
|
by: Martin Johansen |
last post by:
In C,
what do you call that which is separated by semicolon?
what is the difference between an expression and a statement?
Thank you.
|
by: Fredrik Tolf |
last post by:
Take a look at this C snippet:
#include <stdio.h>
int test(int *var)
{
return(*var += 5);
}
int main(void)
|
by: Xiaoshen Li |
last post by:
Dear All,
I am confused with prototypes in C. I saw the following code in a C book:
void init_array_1(int data)
{
/* some code here */
}
|
by: Neroku |
last post by:
Hello, i would like to know what the serious definition of statements
and expressions is:
i know an expression are evaluated to a value, i.e:
1 == 2
5+7
foo( 1,2)
and a statement is...
|
by: Jess |
last post by:
Hello,
Sometimes declarations are all what we need when we define/declare
classes (or functions?), but sometimes we need definitions. I learned
that if we define a class (B) that has an object...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |