Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could the use of the operator keyword in c++ mean here?

This is for an application that is written in C++. Under what circumstances would this line make sense to any of you, in the context of a struct definition (stream is a member variable of type FILE*):

operator FILE*(){return stream;}

I have been going through with a debugger, trying to make sense of it, but I can't seem to make that line of code activate. I have never encountered the operator overload keyword in such a fashion. What can this line of code do?

like image 667
user121615 Avatar asked Oct 22 '15 19:10

user121615


People also ask

What is the use of keyword operator?

The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands.

What does the -> mean in C?

The Arrow(->) operator exists to access the members of the structure or the unions using pointers.

What does the operator mean in C++?

In programming, an operator is a symbol that operates on a value or a variable. Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while - is an operator used for subtraction. Operators in C++ can be classified into 6 types: Arithmetic Operators.

What is the * operator and what does it do?

Multiplication * (Asterisk) Basic arithmetic operator used for multiplication; the result of an arithmetic operator is usually a numeric value. Division / Basic arithmetic operator used for division; the result of an arithmetic operator is usually a numeric value.


1 Answers

This is an implicit conversion operator.

Implicit conversion operators allow a type that would not otherwise implicitly convert to a destination type, the ability to do so. They have the following syntax, where Foo is the class of the object to be implicitly converted, and Bar is the destination class:

class Foo{
public:
    operator Bar(); // allow implicit conversion of Foo objects to Bar
};

The more common instance of this operator is in converting an object to a boolean value as a validity check. This can be seen with the standard library's streams, and smart pointers.

You should note the presence of a variation on the syntax, which prevents the existing conversion, and makes the conversion explicit instead:

class Foo{
public:
    explicit operator Bar(); // allow explicit conversion of Foo objects to Bar
};

This prevents getting bitten by a compiling program when you accidentally feed a type A that can convert into a type B into a function that accepts only B. Sure, that can be what you're going for, but it isn't always, and they decided to add this to the language to help people with a need for an explicit conversion.

With the explicit conversion operator, you can create an object from the origin object either via construction(using it in the construction of an object of the target type) or by explicit casting: B{A}

like image 96
jaggedSpire Avatar answered Sep 30 '22 18:09

jaggedSpire