Class's worse knowledge

Hi dear people i'm a new learning guy i come from a scripting language where no type was needed, after some day i think to know and appreciate that natural workflow. i have a few qustion about class...

if i made a class Myint
how can i make my class to return an integer type itself?

how the std::string class work's in that manner?

1
2
3
4
std::string im;
im = "some text"
std::cout << im  //how can im be returned as *((char*)im)


How i can obtain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
class Myint{
public: 
int me;

void operator = (const int &input){
  me = input;
}
};

int main(void){
  Myint oo;
oo = 10;
std::cout << oo; // cout threat oo as int 
//or
int reali;
reali = oo; //oo returned from Myint class as integer type
return 0;

}


Any help or critique will be treasured.
You can assign an int to MyInt by defining an assignment operator
1
2
3
4
5
6
7
class Myint {
public:
    Myint &operator=(int input) {
        me = input;
    }
    ...
};


To use it like an int, add a conversion operator. I think that's like this:
1
2
3
4
5
class Myint {
public:
   operator int() {return me; } 
    ...
};


Just an other example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

class Myint{
public:
    int toInt() const {return me;}
    Myint& operator=(int input);
    friend std::ostream& operator<<(std::ostream& ost, const Myint& myint);
private:
    int me;
};

Myint& Myint::operator=(int input){
    me = input;
    return *this;
}

std::ostream& operator<<(std::ostream& ost, const Myint& myint) {
    return ost << myint.me;
}

int main()
{
    Myint oo;
    oo = 10;
    std::cout << oo << std::endl; // cout threat oo as int
    //or
    int reali;
    std:: cout << (reali = oo.toInt()) << std::endl; //oo returned from Myint class as integer type

    return 0;
}

Thanks you men, that's what i ackieved
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
class Myint{
public: 
int me;
std::string mo;

void operator = (const int &input){
  me = input;
}
void operator = (const std::string &input){
  mo = input;
}

operator int() {return me;}
operator std::string() {return mo;}

};

int main(void){
  Myint oo;
oo = "ciao";
oo = 15;
// cout threat oo as int 
//or
int reali;
reali = 10; //oo returned from Myint class as integer type

std::cout << ((std::string)oo) << "\n" << oo; 
return 0;

}


the class return a different type from a cast.

thx to dhayden to show me that type can be used as operator
 
 operator int() {return me; }


thx to Enoizat to show me that class can socialize ^^
1
2
3
4
5
friend std::ostream& operator<<(std::ostream& ost, const Myint& myint);
. . .
std::ostream& operator<<(std::ostream& ost, const Myint& myint) {
    return ost << myint.me;
}


i've some other question,
why i shoulda use
 
Myint& operator=(int input)

instead of
 
void operator = (const int &input)
where is the difference and why?
operator overloading follows a specific format.

a = b; says that the equality operator returns a value which is assigned into a. A void = operator couldn't do that.

You can actually invoke these functions like normal functions via a class::operator = (arg) call.

The difference is simply how the value is returned and what is done with it. Calling =(b) with &(b) means b would be modified by its own value, and a would remain untouched. It doesn't work that way. For a = b, the argument to the = operator is b, not a.

Last edited on
a = b; says that the equality operator returns a value which is assigned into a.

No, the assignment operator is responsible for doing the assignment -- by modifying *this as necessary. The return value doesn't have anything to do with it.

Why should I use
T& operator=(U input);
instead of
void operator=(U const& input);


The short answer is to support code like
1
2
3
4
5
void modify(T& foo)
 { foo.change_me(); }
...
T t; U u;
modify(t = u);


More subtly, the first overload (accepting U by value) suggests an implementation of the copy-and-swap idiom.
http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom
http://www.gotw.ca/gotw/059.htm
Last edited on
Why should I use
T& operator=(U input);
instead of
void operator=(U const& input);

Another reason: it lets you say
1
2
Myint a,b;
a = b = 4;


Now granted, this construction and mobzzi's modify(t = u) are kind of rare, but they happen, and returning the reference is a small price to pay for the added flexibility. If the assignment operator is inlined, then the optimizer can tell whether the return value is used and remove the code that returns it when appropriate.
Topic archived. No new replies allowed.