is this a type of Operator overloading..

Discussion in 'C++' started by NR, Oct 2, 2003.

  1. NR

    NR Guest

    Hi Folks..

    Below is cut paste of one of the "class" in my project... which is
    used in
    a composition relationship in many other classes.

    ----------------------------------------------------
    /* class declaration*/
    class Packet_serialNo
    {
    public:
    Packet_serialNo(word16 value = 0);

    ~Packet_serialNo();

    operator word16() { return serialNo;};
    void Set(word16 newVal) { serialNo = newVal;}

    private:
    word16 serialNo;
    };

    /* implementation*/

    Packet_serialNo::packet_serialNo(word16 value) : serialNo(value){}
    Packet_serialNo::~Packet_serialNo(){}
    ---------------------------------------------------

    Can anyone help explain.. how the private member "SerialNo" is gonna
    get initialised to some value when the class's constructor gets
    called???

    Am really unable to understand.. working of the operator overloading
    function
    "operator word16() { return serialNo;};" in this scenario.


    Pls help me understand with ur inputs..

    Nits
     
    NR, Oct 2, 2003
    #1
    1. Advertising

  2. NR wrote:

    > Hi Folks..
    >
    > Below is cut paste of one of the "class" in my project... which is
    > used in
    > a composition relationship in many other classes.
    >
    > ----------------------------------------------------
    > /* class declaration*/
    > class Packet_serialNo
    > {
    > public:
    > Packet_serialNo(word16 value = 0);
    >
    > ~Packet_serialNo();
    >
    > operator word16() { return serialNo;};


    Get rid of that last semicolon. Functions are never terminated with
    semicolons.

    > void Set(word16 newVal) { serialNo = newVal;}
    >
    > private:
    > word16 serialNo;
    > };
    >
    > /* implementation*/
    >
    > Packet_serialNo::packet_serialNo(word16 value) : serialNo(value){}
    > Packet_serialNo::~Packet_serialNo(){}
    > ---------------------------------------------------
    >
    > Can anyone help explain.. how the private member "SerialNo" is gonna
    > get initialised to some value when the class's constructor gets
    > called???


    It is initialized (with the value from the parameter 'value') through
    the member initialization list. What exactly is the problem you have
    with this?

    >
    > Am really unable to understand.. working of the operator overloading
    > function
    > "operator word16() { return serialNo;};" in this scenario.


    That doesn't have anything to do with the initialization of serialNo. Is
    this a separate question?

    This function defines an implicit conversion from an instance of the
    class to the type word16:

    Packet_serialNo p(24);
    word16 w = p; // OK, implicit conversion applied

    void func(word16 param);
    func(p); // OK, implicit conversion applied

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Oct 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    423
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    735
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    733
    Cholo Lennon
    Aug 29, 2007
  4. Martin T.
    Replies:
    7
    Views:
    820
    Martin T.
    Mar 10, 2008
  5. Replies:
    11
    Views:
    562
Loading...

Share This Page