473,466 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

need help with casting operators...

I have two object types ClassA and ClassB

class ClassA {
public:
int data;
operator ClassB()
{
ClassB b;
b.data = data + 1;
return (b);
}
};

class ClassB {
public:
int data;
operator ClassA()
{
ClassB b;
b.data = data - 1;
return(b);
}
}

So... If I say:

ClassA a1, a2;
ClassB b;

a1.data = 5;
b = a1;
a2 = b;

then a2 should be identical to a1 and b should be 6.

First off I can't get this to compile because ClassB isn't know ClassA
at compile time...

Is it necesary to have int data in both classes or can A inherit from B
and vise versa?

Can both the casting operators exist in only on of the classes?

I guess this is very similar to:

float a1, a2;
int b;
a1 = 5.0
b = a1;
a2 = b;

Of course there is a loss of percision in this case.. but the casting
operations work...

help?
TIA
Jul 22 '05 #1
13 1891
JustSomeGuy wrote:
I have two object types ClassA and ClassB
<snip>
First off I can't get this to compile because ClassB isn't know ClassA
at compile time...

Is it necesary to have int data in both classes or can A inherit from
B and vise versa?

Can both the casting operators exist in only on of the classes?

<snip>

I fixed a few logic errors also (ClassB's operator ClassA returned a
ClassB):

class ClassB;

class ClassA {
public:
int data;
operator ClassB();
};

class ClassB {
public:
int data;
operator ClassA();
};

ClassA::operator ClassB()
{
ClassB b;
b.data = data + 1;
return (b);
}

ClassB::operator ClassA()
{
ClassA a;
a.data = data - 1;
return(a);
}

int main()
{
ClassA a1, a2;
ClassB b;

a1.data = 5;
std::cout << "a1.data == " << a1.data << std::endl;

b = a1;
std::cout << "b.data == " << b.data << std::endl;

a2 = b;
std::cout << "a2.data == " << a2.data << std::endl;

return 0;
}

- Pete
Jul 22 '05 #2
JustSomeGuy wrote:
I have two object types ClassA and ClassB

class ClassA {
public:
int data;
operator ClassB()
{
ClassB b;
b.data = data + 1;
return (b);
}
};

class ClassB {
public:
int data;
operator ClassA()
{
ClassB b;
b.data = data - 1;
return(b);
You probably meant

ClassA a;
a.data = data - 1;
return a;

}
}
;

So... If I say:

ClassA a1, a2;
ClassB b;

a1.data = 5;
b = a1;
a2 = b;

then a2 should be identical to a1 and b should be 6.

First off I can't get this to compile because ClassB isn't know ClassA
at compile time...
Yes, that's where you need to separate class definitions and member
function definitions:

struct B;
struct A {
int data;
operator B() const;
};
struct B {
int data;
operator A() const;
};
A::operator B() const
{
...
}
B::operator A() const
{
...
}

Is it necesary to have int data in both classes or can A inherit from B
and vise versa?
Inherit? Depends on your model.

Can both the casting operators exist in only on of the classes?
No.

I guess this is very similar to:

float a1, a2;
int b;
a1 = 5.0
b = a1;
a2 = b;

Of course there is a loss of percision in this case.. but the casting
operations work...


It's because the conversion operators are known to the language.

Victor
Jul 22 '05 #3
"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:40***************@ucalgary.ca...
I have two object types ClassA and ClassB

class ClassA {
public:
int data;
operator ClassB()
{
ClassB b;
b.data = data + 1;
return (b);
}
};

class ClassB {
public:
int data;
operator ClassA()
{
ClassB b;
b.data = data - 1;
return(b);
}
}

So... If I say:

ClassA a1, a2;
ClassB b;

a1.data = 5;
b = a1;
a2 = b;

then a2 should be identical to a1 and b should be 6.

First off I can't get this to compile because ClassB isn't know ClassA
at compile time...

Is it necesary to have int data in both classes or can A inherit from B
and vise versa?

Can both the casting operators exist in only on of the classes?

I guess this is very similar to:

float a1, a2;
int b;
a1 = 5.0
b = a1;
a2 = b;

Of course there is a loss of percision in this case.. but the casting
operations work...


#include <iostream>
#include <ostream>

class B;

class A
{
int data;

public:
A(int d = 0) : data (d) { }
operator B() const;
friend std::ostream& operator<<(std::ostream& os, const A& a)
{
return os << a.data;
}
};

class B
{
int data;

public:
B(int d = 0) : data (d) { }

operator A() const { return data - 1; }
friend std::ostream& operator<<(std::ostream& os, const B& b)
{
return os << b.data;
}
};

A::operator B() const { return data + 1; }

int main()
{
A a1(5);
A a2;
B b;

b = a1;
a2 = b;

std::cout << "a1 == " << a1 << '\n'
<< "a2 == " << a2 << '\n'
<< "b == " << b << '\n';

return 0;
}

Output:

a1 == 5
a2 == 5
b == 6
-Mike
Jul 22 '05 #4
In article <40***************@ucalgary.ca>, JustSomeGuy wrote:
I have two object types ClassA and ClassB

class ClassA {
public:
int data;
operator ClassB()
{
ClassB b;
b.data = data + 1;
return (b);
}
};

class ClassB {
public:
int data;
operator ClassA()
{
ClassB b;
I assume you meant ClassA b; ?
b.data = data - 1;
return(b);
}
}
There should be a ; here.

So... If I say:

ClassA a1, a2;
ClassB b;

a1.data = 5;
b = a1;
a2 = b;

then a2 should be identical to a1 and b should be 6.

First off I can't get this to compile because ClassB isn't know ClassA
at compile time...
class ClassB; //forward declaration of ClassB

class ClassA {
public:
int data;
operator ClassB(); //OK ClassB forward declared.
};

class ClassB {/*as declared above*/};

inline ClassA::operator ClassB()
{
ClassA b;
b.data = data + 1;
return b;
}
Is it necesary to have int data in both classes or can A inherit from B
and vise versa?
If ClassB is derived from ClassA, then a ClassB object would contain a
ClassA subobject, and also it's data member. Or vice versa. Or both
classes could be derived from a common base class. What do you want to
do?
Can both the casting operators exist in only on of the classes?


Yes. It's called a constructor in the other class:

ClassB::ClassB(const ClassA &b) : data(b.data + 1) {}

--
Robert Bauck Hamar
Jul 22 '05 #5
Robert Bauck Hamar wrote:
In article <40***************@ucalgary.ca>, JustSomeGuy wrote:
I have two object types ClassA and ClassB

class ClassA {
public:
int data;
operator ClassB()
{
ClassB b;
b.data = data + 1;
return (b);
}
};

class ClassB {
public:
int data;
operator ClassA()
{
ClassB b;
I assume you meant ClassA b; ?
b.data = data - 1;
return(b);
}
}


There should be a ; here.

So... If I say:

ClassA a1, a2;
ClassB b;

a1.data = 5;
b = a1;
a2 = b;

then a2 should be identical to a1 and b should be 6.

First off I can't get this to compile because ClassB isn't know ClassA
at compile time...


class ClassB; //forward declaration of ClassB

class ClassA {
public:
int data;
operator ClassB(); //OK ClassB forward declared.
};

class ClassB {/*as declared above*/};

inline ClassA::operator ClassB()
{
ClassA b;
b.data = data + 1;
return b;
}
Is it necesary to have int data in both classes or can A inherit from B
and vise versa?


If ClassB is derived from ClassA, then a ClassB object would contain a
ClassA subobject, and also it's data member. Or vice versa. Or both
classes could be derived from a common base class. What do you want to
do?
Can both the casting operators exist in only on of the classes?


Yes. It's called a constructor in the other class:

ClassB::ClassB(const ClassA &b) : data(b.data + 1) {}


Ok thats good... but why then not use constructors in both cases rather than
casting operators?
Thanks all!


--
Robert Bauck Hamar


Jul 22 '05 #6
JustSomeGuy wrote:
Robert Bauck Hamar wrote:
<snip>
Is it necesary to have int data in both classes or can A inherit
from B and vise versa?


If ClassB is derived from ClassA, then a ClassB object would contain
a ClassA subobject, and also it's data member. Or vice versa. Or
both classes could be derived from a common base class. What do you
want to do?
Can both the casting operators exist in only on of the classes?


Yes. It's called a constructor in the other class:

ClassB::ClassB(const ClassA &b) : data(b.data + 1) {}


Ok thats good... but why then not use constructors in both cases
rather than casting operators?
Thanks all!


Robert is wrong; you can have a casting operator in both classes. See the
other replies.

- Pete


--
Robert Bauck Hamar


Jul 22 '05 #7
In article <uS*******************@newsread1.news.pas.earthlin k.net>, Petec wrote:
JustSomeGuy wrote:
Robert Bauck Hamar wrote:
Can both the casting operators exist in only on of the classes?

Yes. It's called a constructor in the other class:

ClassB::ClassB(const ClassA &b) : data(b.data + 1) {}


Ok thats good... but why then not use constructors in both cases
rather than casting operators?
Thanks all!


Robert is wrong; you can have a casting operator in both classes. See the
other replies.


I believe one of us is misunderstanding the OP. I have not stated that you
cannot have conversion functions[0] in both classes.

Assume:

class A {/*...*/} a;
class B {/*...*/};

There are now two ways to make the definition

B b = a;

legal:
* One can define the conversion function A::operator B(), or
* one can define a constructor in B that can be called with a single
argument of type A. This constructor must not be marked explicit.

This is then called a user defined conversion. Why choose one
over the other? Maybe you can change class B but not class A, or maybe
the implementation of the classes makes one of the choices easier. Or
if you are defining a conversion between your class and some fundamental
type.

[0]: A 'conversion function' is defined by the standard to be a
memberfunction of a class, with the signature 'operator T()' where T is
a type.
--
Robert Bauck Hamar
Jul 22 '05 #8

"Robert Bauck Hamar" <ro**********@ifi.uio.no> wrote in message
news:sl*************************@tyrfing.ifi.uio.n o...
In article <uS*******************@newsread1.news.pas.earthlin k.net>, Petec wrote:
JustSomeGuy wrote:
Robert Bauck Hamar wrote:
> Can both the casting operators exist in only on of the classes?

Yes. It's called a constructor in the other class:

ClassB::ClassB(const ClassA &b) : data(b.data + 1) {}
Ok thats good... but why then not use constructors in both cases
rather than casting operators?
Thanks all!


Robert is wrong; you can have a casting operator in both classes. See the other replies.


I believe one of us is misunderstanding the OP. I have not stated that

you cannot have conversion functions[0] in both classes.

Assume:

class A {/*...*/} a;
class B {/*...*/};

There are now two ways to make the definition

B b = a;

legal:
* One can define the conversion function A::operator B(), or
* one can define a constructor in B that can be called with a single
argument of type A. This constructor must not be marked explicit.

This is then called a user defined conversion. Why choose one
over the other? Maybe you can change class B but not class A, or maybe
the implementation of the classes makes one of the choices easier. Or
if you are defining a conversion between your class and some fundamental
type.

[0]: A 'conversion function' is defined by the standard to be a
memberfunction of a class, with the signature 'operator T()' where T is
a type.
--
Robert Bauck Hamar


Yes I've pretty much come to the same conclusion myself that I could have
implemented the copy constructor with the other class as a parameter...
However
I'm not sure that guarantees that if I call a function whose prototype is
myfunc(ClassA &a) with an instance of ClassB as the paramter, that the cast
operator to convert ClassB to ClassA will be invoked.


Jul 22 '05 #9
In article <QZOxc.721949$Ig.492597@pd7tw2no>, JustSomeGuy wrote:
Yes I've pretty much come to the same conclusion myself that I could have
implemented the copy constructor with the other class as a parameter...
However
I'm not sure that guarantees that if I call a function whose prototype is
myfunc(ClassA &a) with an instance of ClassB as the paramter, that the cast
operator to convert ClassB to ClassA will be invoked.


No it should not. However if the prototype is myfunc(const ClassA &a),
then you would be able to call myfunc(b), which means
myfunc(ClassA::ClassA(b))
or
myfunc(b.operator ClassA()).
--
Robert Bauck Hamar
Jul 22 '05 #10
hi there

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:alJxc.8079
[...]
#include <iostream>
#include <ostream>

class B;

class A
{
int data;

public:
A(int d = 0) : data (d) { }
operator B() const;
friend std::ostream& operator<<(std::ostream& os, const A& a)
{
return os << a.data;
}
};

class B
{
int data;

public:
B(int d = 0) : data (d) { }

operator A() const { return data - 1; }
friend std::ostream& operator<<(std::ostream& os, const B& b)
{
return os << b.data;
}
};

A::operator B() const { return data + 1; }

int main()
{
A a1(5);
A a2;
B b;

b = a1;
a2 = b;

std::cout << "a1 == " << a1 << '\n'
<< "a2 == " << a2 << '\n'
<< "b == " << b << '\n';

return 0;
}

Output:

a1 == 5
a2 == 5
b == 6


i m bit confuse about all these examples, can u please explain it to me, or
point me to some good online resources :-)

class A
{
....
operator B() const;
....
};

here are we overloading Constructor of B ???? as operator ?? in class "A" i
m really confuse...and really dont know what to say and how to phrase my
question....
Jul 22 '05 #11

"dumboo" <vt***@yahoo.com> wrote in message
news:2i************@uni-berlin.de...
hi there

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:alJxc.8079
[...]
#include <iostream>
#include <ostream>

class B;

class A
{
int data;

public:
A(int d = 0) : data (d) { }
operator B() const;
friend std::ostream& operator<<(std::ostream& os, const A& a)
{
return os << a.data;
}
};

class B
{
int data;

public:
B(int d = 0) : data (d) { }

operator A() const { return data - 1; }
friend std::ostream& operator<<(std::ostream& os, const B& b)
{
return os << b.data;
}
};

A::operator B() const { return data + 1; }

int main()
{
A a1(5);
A a2;
B b;

b = a1;
a2 = b;

std::cout << "a1 == " << a1 << '\n'
<< "a2 == " << a2 << '\n'
<< "b == " << b << '\n';

return 0;
}

Output:

a1 == 5
a2 == 5
b == 6
i m bit confuse about all these examples, can u please explain it to me,

or point me to some good online resources :-)
There's only one online book I recommend:
Bruce Eckel's "Thinking in C++"
www.mindview.net

I strongly recommend getting some 'regular' books. See the
book reviews at www.accu.org
class A
{
...
operator B() const;
...
};

here are we overloading Constructor of B ????
No. A constructor always has the same name as
its class [1] e.g. a constructor for class A
would look like:

A(/* optional parameters */) /* constructor for class 'A' */
{
/* etc */
}
as operator ?? in class "A" i
m really confuse...and really dont know what to say and how to phrase my
question....


The function above ('operator B()') is known as a 'conversion
function' (sometimes called a 'conversion operator'. It converts
a type 'A' (the enclosing class type) to a type 'B' (indicated
by the 'B' in 'operator B()'. Conversion functions may not have
a return type or any parameters (the return type is indicated by
the function's name, and parameters would meet any need.)

The above allows code such as:

A a;
B b;

b = a; /* converts a type 'A' to a type 'B' */

Classes can also define conversion functions that convert
to built-in types, e.g.

operator int() { /* etc */ }

[1] Technically, constructors don't actually have names, but I find
it makes it easier to explain to a novice, since the syntax makes
it look like it has a name (the same as that of its class).

-Mike
Jul 22 '05 #12
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:X%*****************@newsread2.news.pas.earthl ink.net...
Conversion functions may not have
a return type or any parameters (the return type is indicated by
the function's name, and parameters would
not
meet any need.)

oops!

-Mike
Jul 22 '05 #13
thanks Mike for giving ur time, will definately go through that book :-)
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
3
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T>...
2
by: dave | last post by:
Why is this the same: (*(*pCurrent).pData).name as this: pCurrent->pData->name what is the benefit to the first? if any? why are the parenthesis important? thanks
5
by: Tom Carroll | last post by:
When assigning or testing equality between a type and a constant, should the constant be casted to the type? I.e. should fork() == -1 be written as fork() == (pid_t)-1? should uid = 45 be...
0
by: bob | last post by:
Hello, I have another question I have this hierarchy: Matrix MatrixMxN Matrix6x6 Vector Vector4D Vector2D
9
by: john | last post by:
I thought about this and I just want confirmation on that it's right If I do like this double b = 10.6; double c = 5.0; double a = (int)b / c; This will cast b to an int, and then divide it...
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
6
by: Nishu | last post by:
Hi all, What is difference between typecasting and casting? I used to think that both are same; but few days back someone pointed out here that these are different. Vague guess, Is it that...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.