Thread: ? about this> index::index () : type_intex()

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    ? about this> index::index () : type_intex()

    Hello,

    Can someone tell me what this statement is called, the type_index statement, or how it is used please?

    () : type_index()

    Code:
    in the following context
    
    index::index () : type_index()
    {
        memset(icmomiscp,'\0',sizeof(ICMOMISC));
     
    }
    Thanks very much!
    bk

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    After defining a function like that, you can either initialize your variables in the {} or you can put the colon and initialize whatever you want. This is just calling a function to initialize something. The “: code here” is called the initialization section.

    Code:
    Example::Example() : temp = 0
    {
    //left blank on purpose
    }}
    is the same thing as

    Code:
    Example::Example()
    {
    temp = 0
    }

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    Great! thanks alot that's pretty cool!

    Have a nice weekend!

    bk

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Syntactically Enahs' example is illegal, it should use a constructor format: temp(0). In your example, type_index might be a member variable, and the empty parenthesis is default-initializing it (to 0 for a built-in or POD type, and with the default constructor for a class type). Also, type_index could be a base class that is being default-initialized.

    Generally using the initializer list is preferred to assigning an initial value inside the constructor block. That is because some members must be initialized in the initializer list, like reference members, and const members. Also, for some class members it is more efficient to use the initializer list, since the class's constructor is called whether you initialize it there or not, and if you assign a new value inside the containing class's constructor body, you are wasting the cpu cycles spent initializing the member class in its default constructor.

  5. #5
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    example is illegal, it should use a constructor format: temp(0).
    He is right.
    I have been arguing with me teacher over:
    example = 5;
    Or
    example(5);

    For a diff situation….I got points deducted off an assignment for using “example(5)” and argue a lot with the teacher about it. But alas, you must do what the teacher said so I have been forcing my self to do the =X way….bad time to relapse sorry!

  6. #6
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    hi..i'm still in learning phase, and i just learned that
    x=6 is the same as x(6)
    infact the x(6) is the constructor method of initialising a class...so initialising a variable like this is legal too!

    well, that's what i just learned, plz if i'm wrong somewhere, do let me know..thanks for ur attention!

    Kind Regards,
    wakish

Popular pages Recent additions subscribe to a feed