Need help

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

    #1

    Need help

    Hi,
    I am new to C++ .Can anyone please tell me what does ":" operator
    mean in C++.For ex: there is a program snippet which looks like that

    class convert {
    protected:
    double val1; // initial value
    double val2; // converted value
    public:
    convert(double i) {
    val1 = i;
    }
    virtual void compute() = 0;
    };
    // Liters to gallons.
    class l_to_g : public convert {
    public:
    l_to_g(double i) : convert(i) { }
    void compute() {
    val2 = val1 / 3.7854;
    }
    };

    In the above piece of code there is a line as
    l_to_g(double i) : convert(i) { }

    Can anybody tell me wht does the above line mean

    Thanks
    Ravi

  • Marco Wahl

    #2
    Re: Need help

    "ravi" <ravindra634@gm ail.comwrites:
    I am new to C++ .Can anyone please tell me what does ":" operator
    mean in C++.For ex: there is a program snippet which looks like that
    ":" is merely a seperator. Your question is basic and
    should be answered in C++-learning books.

    Anyway, after reindenting and commenting some your code I come to the
    following code:

    class convert {
    protected:
    double val1; // initial value
    double val2; // converted value
    public:
    convert(double i) {
    val1 = i;
    }
    virtual void compute() = 0;
    };

    class l_to_g : // Seperator for the baseclass-list,
    // here class convert.
    public convert {
    public:
    l_to_g(double i) : // After this seperator follows
    // the initializer-list,
    // e.g. initialize the base-class
    // convert.
    convert(i) { }
    void compute() {
    val2 = val1 / 3.7854;
    }
    };
    >
    In the above piece of code there is a line as
    l_to_g(double i) : convert(i) { }
    >
    Can anybody tell me wht does the above line mean
    Hopefully the comments in the code above help you.


    Best wishes

    Comment

    • David Harmon

      #3
      Re: Need help

      On 29 Aug 2006 23:39:21 -0700 in comp.lang.c++, "ravi"
      <ravindra634@gm ail.comwrote,
      >In the above piece of code there is a line as
      l_to_g(double i) : convert(i) { }
      >
      >Can anybody tell me wht does the above line mean
      This particular ":" is an example of the "constructo r initializer
      list". It passes information to the constructors of members or (as
      in this example) base classes that are created _before_ the
      constructor body runs.

      This issue is mentioned in Marshall Cline's C++ FAQ. See the topic
      "[10.6] Should my constructors use "initializa tion lists" or
      "assignment ". It is always good to check the FAQ before posting.
      You can get the FAQ at:


      Please see also the topic "[5.7] What makes a good Subject: line?"

      Comment

      • Bart

        #4
        Re: Need help

        ravi wrote:
        <snip>
        In the above piece of code there is a line as
        l_to_g(double i) : convert(i) { }
        >
        Can anybody tell me wht does the above line mean
        This is a constructor initializer list. It initializes the members and
        base clases. Normally, when you have a base class with a default
        constructor you can just omit this initializer. For example:

        class Base
        {
        public:
        Base(); // default constructor
        };

        class Derived : public Base
        {
        public:
        Derived() {} // Don't need to initialize the base class. Default
        constructor available.
        };

        But when there is no default constructor you need to explicitly
        initialize any base class without one. You also need to explicitly
        initialize members that need an initializer, like references and const
        members. You can separate the initializers with commas, like so:

        MyClassConstruc tor()
        : member1(/* something */)
        , member2(/* something */)
        , member3(/* something */)
        {
        }

        Generally, it is better form to initialize your members in the
        initializer list rather than using assignments inside the body of the
        constructor.

        Regards,
        Bart.

        Comment

        Working...