are these statements declarations or definitions?

Discussion in 'C++' started by Jess, Aug 22, 2007.

  1. Jess

    Jess Guest

    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
     
    Jess, Aug 22, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 22, 2007
    #2
    1. Advertising

  3. Jess

    Jess Guest

    On Aug 22, 11:43 pm, "Victor Bazarov" <> wrote:
    > 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<A> pA(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<A> object.

    Thanks,
    Jess
     
    Jess, Aug 26, 2007
    #3
  4. Jess wrote:
    > [..] If I'd like to define a shared_ptr object using
    >
    > std::tr1::shared_ptr<A> pA(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<A> object.


    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
     
    Victor Bazarov, Aug 26, 2007
    #4
  5. Jess

    Jess Guest

    On Aug 26, 12:18 pm, "Victor Bazarov" <> wrote:
    > Jess wrote:
    > > [..] If I'd like to define a shared_ptr object using

    >
    > > std::tr1::shared_ptr<A> pA(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<A> object.

    >
    > 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
     
    Jess, Aug 29, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rikard Land
    Replies:
    0
    Views:
    348
    Rikard Land
    Oct 19, 2004
  2. Steven T. Hatton

    Declarations and Definitions in One Header

    Steven T. Hatton, Apr 25, 2004, in forum: C++
    Replies:
    4
    Views:
    327
    Steven T. Hatton
    Apr 25, 2004
  3. Chris Gordon-Smith

    Declarations and Definitions: Grammar

    Chris Gordon-Smith, Oct 3, 2004, in forum: C++
    Replies:
    2
    Views:
    369
    Gary Labowitz
    Oct 3, 2004
  4. aaragon
    Replies:
    14
    Views:
    406
  5. Jess
    Replies:
    15
    Views:
    506
Loading...

Share This Page