problem in declaration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Lindauer

    #1

    problem in declaration

    If I do this:

    struct xyz
    {
    xyz();
    int a,b;
    } var1;

    it compiles.

    But if I do this

    struct xyz
    {
    xyz();
    int a,b;
    } var2();

    it doesn't compile. The comeau compiler says var2() is a function
    rather than a variable initialization. ..

    but then I can do this:

    struct xyz
    {
    xyz();
    int a,b;
    } ;
    struct xyz var3();

    and that compiles.

    What is the rational for the difference in behavior?

    Thanks,

    David

  • Victor Bazarov

    #2
    Re: problem in declaration

    David Lindauer wrote:[color=blue]
    > If I do this:
    >
    > struct xyz
    > {
    > xyz();
    > int a,b;
    > } var1;
    >
    > it compiles.
    >
    > But if I do this
    >
    > struct xyz
    > {
    > xyz();
    > int a,b;
    > } var2();
    >
    > it doesn't compile. The comeau compiler says var2() is a function
    > rather than a variable initialization. ..[/color]

    Yes, it is.
    [color=blue]
    > but then I can do this:
    >
    > struct xyz
    > {
    > xyz();
    > int a,b;
    > } ;
    > struct xyz var3();
    >
    > and that compiles.
    >
    > What is the rational for the difference in behavior?[/color]

    Types are not allowed to be _defined_ in a function declaration.

    V
    --
    Please remove capital As from my address when replying by mail


    Comment

    • Rolf Magnus

      #3
      Re: problem in declaration

      David Lindauer wrote:
      [color=blue]
      > If I do this:
      >
      > struct xyz
      > {
      > xyz();
      > int a,b;
      > } var1;
      >
      > it compiles.
      >
      > But if I do this
      >
      > struct xyz
      > {
      > xyz();
      > int a,b;
      > } var2();
      >
      > it doesn't compile. The comeau compiler says var2() is a function
      > rather than a variable initialization. ..
      >
      > but then I can do this:
      >
      > struct xyz
      > {
      > xyz();
      > int a,b;
      > } ;
      > struct xyz var3();
      >
      > and that compiles.
      >
      > What is the rational for the difference in behavior?[/color]

      If it looks like a function declaration, it is a function declaration.
      Your second example is similar to:

      type name();

      which clearly looks like a function declaration.

      Comment

      Working...