Nov 5, 2010 at 8:35am UTC
Hello guys,
i need a simple code exemple with a overloaded cast operator.
I need to call something like this:
myClass C;
C = (myClass) 20;
where
class myClass {
int x, y;
// and the rest of code
}
and it will set the object proprieties to x=20, y=20
Thanks a lot !
Nov 5, 2010 at 10:22am UTC
If you are doing C = (myClass) 20, you need to overload the int cast operator not your class.
What you need is:
1 2 3 4 5 6 7 8 9 10 11 12
class myClass {
int x, y;
myClass & operator = (int );
}
myClass & myClass ::operator = (int asi)
{
this ->a = asi;
this ->b = asi;
return *this ;
}
This will allow you to do:
C = 20
Last edited on Nov 5, 2010 at 10:23am UTC
Nov 5, 2010 at 2:40pm UTC
This is actually done by making a constructor, not overloading a cast operator.
Overloading the assignment operator per Ragger's suggestion could also work but is less effective.
Nov 5, 2010 at 4:56pm UTC
I know i could overload the assignment operator and to call C = 20, but i really need an example that overloads the typecast operator... and to call C = (myClass) 20. It's a homework for school.
Nov 5, 2010 at 5:37pm UTC
You have your concepts backwards.
Constructors construct an object from a type.
typecast operators produce a type from a constructed object.
Basically:
constructor: takes an int, gives you a myClass
typecast operator: takes a myClass, gives you an int
In this case, you can't make a typecast operator that does what you describe. You'd have to do that with a constructor.
Nov 6, 2010 at 11:08am UTC
ok... meybe it wasn't the best exemple. I will be more specific this time.
#include <iostream.h>
class A {
public:
int x;
A();
operator B();
};
A::A() {
x=1;
}
A::operator B() { B obj1; obj1.x=x; obj1.y=1;return obj1; };
class B: public A {
public:
int y; // int x inherit from class A
B();
echo();
//operator B();
};
//B::operator B() {B obj1; obj1.x = this->x; obj1.y=1; return B;}
B::B() {
x=1;
y=1;
}
B::echo() {
cout<<"x="<<x<<" si y="<<y<<endl;
}
int main () {
B obj1;
A obj2;
obj1=(B) obj2;
obj1.echo();
return 0;
}
So i need a way to convert an object from class A to an class B object.
I also made the following code:
#include <iostream.h>
class B {
public:
int x, y;
B();
echo();
//operator B();
};
//B::operator B() {B obj1; obj1.x = this->x; obj1.y=1; return B;}
B::B() {
x=1;
y=1;
}
B::echo() {
cout<<"x="<<x<<" si y="<<y<<endl;
}
class A {
public:
int x;
A();
operator B();
};
A::A() {
x=100;
}
A::operator B() { B obj1; obj1.x=x; obj1.y=1;return obj1; };
int main () {
B obiect1;
A obiect2;
obiect1=(B) obiect2;
obiect1.echo();
return 0;
}
This works, to define first the class B and second the class A, but the class B then won't inherit from class A.
Thank you for answers.
Nov 6, 2010 at 4:08pm UTC
1) Again, this is better off as a constructor for B. Not as a cast operator for A.
2) This seems like a questionable/bad design anyway. If B inherits from A you probably shouldn't be able to construct a B from an A. Especially not implicitly like this. That would be extremely error prone.
What are the actual classes you're working with here? I don't need the full bodies of them, just their name and what they're supposed to represent.
Maybe if I can see exactly what it is you're trying to do I can give you an idea for a better design.