Initialising struct in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Natasha26
    New Member
    • Mar 2008
    • 12

    Initialising struct in C++

    I was playing around with struct and realised that when i inherit from a base-class, i can no longer initialise the inherited struct the way i'm used to.

    Code:
    struct Date{[indent]int dd, mm, yy;[/indent]
    }
    
    struct Employee{[indent]char* fname, *lname;
    Date hiring_d;
    short dept;
    [/indent]
    };
    
    struct Manager : public Employee{[indent]short level; //i've tried commenting this line for furher testing but no luck[/indent]
    };
    Here's how i initialise:
    Code:
    int main()
    {[indent]
    Employee e1 = {"Snoop", "Dogg", ' ', {25,3,2008}, 101}; //this is ok
    Manager m1 = {"Gin", "Juice", '& ', {01,01,2000}, 101, 2}; //this fails
    [/indent]
    }
    I use Borland free compiler 5.5. And the error i get is:
    Error E2190 Ch12DerivedClas s.cpp 43: Unexpected }

    Any idea? It's probably not possible to initialise derived structures like that? Nat.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I think there is a missing semicolon in the structure declaration.

    [code=cpp]
    struct Date{
    int dd, mm, yy;
    };//Semicolon was missing here

    struct Employee{
    char* fname, *lname;
    Date hiring_d;
    short dept;
    };

    [/code]


    Raghuram

    Comment

    • Natasha26
      New Member
      • Mar 2008
      • 12

      #3
      Originally posted by gpraghuram
      I think there is a missing semicolon in the structure declaration.

      [code=cpp]
      struct Date{
      int dd, mm, yy;
      };//Semicolon was missing here

      struct Employee{
      char* fname, *lname;
      Date hiring_d;
      short dept;
      };

      [/code]


      Raghuram
      I wrongly copied & pasted my code. Sorry about that, but that's not the problem. I can't initialise the inherited structure using ={.,.,..};

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by Natasha26
        I wrongly copied & pasted my code. Sorry about that, but that's not the problem. I can't initialise the inherited structure using ={.,.,..};


        the following declaration works after some modification
        [code=cpp]
        Employee e1 = {"Snoop", "Dogg", {25,3,2008}, 101}; //Removed the ' '
        [/code]

        But you cant initialize the manager object as it is inheriting the other structure and you shold have a constructor to do this.
        Other members please comment on this

        Raghuram

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          To Natasha26: classes are implemented as structs in C++. The only difference is the default access to member of a class is private whereas for a struct it is public.

          That is, if you specify private/public/protected for every member of your struct it is identical to a class.

          That means, you use construtors, destructors, etc with your structs.

          That means, you initialize your base struct by using an initialization list in the constructor of the derived struct.

          Comment

          • Natasha26
            New Member
            • Mar 2008
            • 12

            #6
            Originally posted by weaknessforcats
            To Natasha26: classes are implemented as structs in C++.

            //....

            That means, you initialize your base struct by using an initialization list in the constructor of the derived struct.
            Well spotted weaknessforcats ! Also thank you Raghuram. Cheers guys. T.

            Comment

            Working...