Thread: What is this?

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401

    What is this?

    I found the following code snippet in a book, and it solved my initial problem, but now I'm kinda wondering...what does it do?
    PHP Code:
    struct command_struct
    {
        
    string name;
        
    bool msg;
        
        
    //initializes command_struct
        // (1)
        
    command_struct(string xbool a) : name(x), msg(a){}
    };

    command_struct cmdArray[] =
    {
        
    // (2)
        
    command_struct("say"true),
        
    command_struct("NULL"false)
    }; 
    [list=1][*]What does the line right after comment (1) do?[*]What does the line right after comment (2) do?[/list=1]I've tried looking it up, but I'm not having much luck seeing as I'm not real sure what to look under. To me, the line after (2) looks a lot like a parameterized constructor, but the line after (1) has me very stumped. Any ideas?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >What does the line right after comment (1) do?

    It's a constructor. Classes & structs in C++ are the same, except that with structs, by default, everything is public, but with classes everything is private.


    >What does the line right after comment (2) do?

    It's initailising an array of two structs, by calling the constructors.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    In C++, we have a choice of syntax for initializing variables in the constuctor. The one in your snippet is called the "preamble", but it does the same thing as this:

    command_struct(string x, bool a) {
    name = x, msg = a;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Okay, thanks. That's what it was acting like it was doing, but I just wanted to make sure. I'd never seen this 'preamble' form before. Learning things every day...
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Hello,

    In C++, we have a choice of syntax for initializing variables in the constuctor. The one in your snippet is called the "preamble", but it does the same thing as this:

    command_struct(string x, bool a) {
    name = x, msg = a;
    }
    Quite wrong. This code will call the default constructor for name then use the assignment operator to assign x to name. It is always better to initialize members in the initializer list. It doesn't matter for built-in types but I suggest doing it for them too to be consistent.
    - lmov

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Thumbs up

    Excellent point.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It is always better to initialize members in the initializer list.
    It's not always better, but it's never worse. The initialization list should be preferred.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed