is this a type of Operator overloading..

N

NR

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
 
K

Kevin Goodsell

NR said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top