Help with struct

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

    Help with struct

    Hi, could someone please help me understand the following code:

    Code:
    struct TypeClass
    {   TypeClass (Type t, const char **c) :: t(t), c(c) {}
    };
    Advanced thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, this code won't compile. The :: needs to be a single colon.

    This is a struct TypeClass that has a constructor that takes Type t and a char** c as arguments. The code after which should be one colon is the initializer list and t(t) call constuctor on the Type class that takes a Type argument, that the copy constructor.

    However, for that typo work, you need a TypeClass member of type Type named t. Otherwise, that ain't going rto compile.

    Ditto for the c.

    Remember, classes in C++ are implemented as struct. If you declare a struct and specify public/private/protected for each struct member, your struct is identical to a class.

    The only difference betwen the struct and the class is that the default access for struct is public whereas for a class it is private. There are no other differences.

    Comment

    • unclefester
      New Member
      • Mar 2008
      • 12

      #3
      Originally posted by weaknessforcats
      First, this code won't compile. The :: needs to be a single colon.

      This is a struct TypeClass that has a constructor that takes Type t and a char** c as arguments. The code after which should be one colon is the initializer list and t(t) call constuctor on the Type class that takes a Type argument, that the copy constructor.

      However, for that typo work, you need a TypeClass member of type Type named t. Otherwise, that ain't going rto compile.

      Ditto for the c.

      Remember, classes in C++ are implemented as struct. If you declare a struct and specify public/private/protected for each struct member, your struct is identical to a class.

      The only difference betwen the struct and the class is that the default access for struct is public whereas for a class it is private. There are no other differences.
      Thanks so much weaknessforcats =) Could you clarify your explanation "The code after which should be one colon is the initializer list and t(t) call constuctor on the Type class that takes a Type argument, that the copy constructor."? I didn't quite get that. Thanks.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The initializizatio n list for a class constrcutor follows a single colon placed after the right parenthesis. For example:

        [code=cpp]
        MyClass::MyClas s(int arg) : mvar(arg) {}
        [/code]

        Here the value of arg is used to initialize the member mvar when the member is created. This is how you pass constructor arguments to your member variables:

        [code=cpp]
        class MyClass
        {

        Date dt;

        public:
        MyClass(int m, int d, int y);
        };
        MyClass::MyClas s(int m, int d, int y) : dt(m,d,y) {}
        [/code]

        The dt(m,d,y) calls Date::Date(int month, int day, int year).

        Remember, C++ objects are initialized memberwise and you may need to call member variable constructors to get your object properly built.

        Comment

        Working...