Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Name for an operator

28 views
Skip to first unread message

Paul

unread,
May 17, 2015, 4:31:59 PM5/17/15
to
This code (below) is copied from another website. I'd like to know the name of the technique of creating operators by the mechanism: A::operator B() so that I can research this independently. What is the name of this methodology for creating the member function of struct A?

Thank you,

Paul

#include <iostream>
struct B;
struct A {
operator B();
};

struct B {
B() { }
B(A const&) { std::cout << "<direct> "; }
};

A::operator B() { std::cout << "<copy> "; return B(); }

int main() {
A a;
B b1(a); // 1)
B b2 = a; // 2)
}
Message has been deleted

Richard

unread,
May 17, 2015, 10:39:46 PM5/17/15
to
[Please do not mail me a copy of your followup]

Paul <peps...@gmail.com> spake the secret code
<242662a2-9a42-4458...@googlegroups.com> thusly:

>#include <iostream>
>struct B;
>struct A {
> operator B();
>};

struct A is defining a conversion operator so that it can be converted
into a struct B. This enables the following:

A a;
B b1 = a; // implicit conversion
B b2 = static_cast<B>(a); // explicit conversion

If you change to:

struct B;
struct A {
explicit operator B();
};

A a;
B b1 = a; // error: implicit conversion not allowed
B b2 = static_cast<B>(a); // OK: explicit conversion
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages