Implicit overloading in C ++

full disclosure is for homework. And I usually do not ask for help at home, but here it is.

I am invited to provide 5 examples of "implicit overloads in C ++". I am sure that he has in mind operator overloading for types such as char, int, float, etc. In iostream, the types themselves.

I understand clearly overloading operators like (my dumb example)

class Vegetables {
    public:
        Vegetables();
        ~Vegetables();
        Vegetables& operator+ (Vegetables&);
        Vegetables& operator- (Vegetables&);
    private:
        int beans;
        ... // more veggies here
};

Vegetables& Vegetables::operator+ (Veggies&) {
    beans += Veggies.beans;
    ...
    return *this;
}

, , "" . , int + double. , , int cast double, double + double. , , , , iostream - .. ...

+3
1

.

. . ...

" ++" .

-, . , . , ( ) , . . , . . , , . . ! , , .

: Google. .

: , () , , . :

class Foo
{
public:
  int i;
  Foo(int argI) : i(argI) { }
  //Foo( const Foo & argFoo ) : i(argFoo.i) { }
};

int
main()
{
  Foo f(2),g(3);
  Foo h = f;
}

, f (2) g (3), Foo (int). Foo h = f h , Foo (int). (Caveat Emptor: gcc/g++ 3.4.4 4.0.1. ...)

, operator =() , .

: (), , . , g (3.2), . int Foo (int). ( [should!] .)

. Foo :

operator int() { return i; }

:

cout << "Foo h = " << h << endl;

h int.

. double/long-double/float int/short/long/etc signed/unsigned .. (, , . -1 unsigned ... , , !)

(void *). :

void voidFunction (void * v) { cout << "v= " << v << endl; }

int main() { Foo z(2); voidFunction(&z); }

, , , , .

, / .

, (printf (format,...)). (, #include < stdarg.h > . va_start(), va_arg(), va_end().) , , , , ...

, #define FOO (X), ... , . , , , ...

templating, , , ...

: , ... !

+4

Source: https://habr.com/ru/post/1770441/


All Articles