answersLogoWhite

0


Best Answer

You can't overload the insertion operator (<<) from inside a class because the l-value must be an ostream object, but operator overloads implemented within classes always implicate the class instance itself as being the l-value.

You must overload the insertion operator from outside of the class, like so:

ostream& operator<<(ostream& lhs, const MyObject& rhs)

{

lhs << rhs.get_data();

return( lhs );

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the declaration of the function to overload output operator inside class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is local declaration?

Declarations inside a function is called local declaration


What is a global declaration?

A global declaration of a function or variable is a declaration that is at the global scope of the program, not inside another function. This means that the name will be visible within all functions in the program.


Explain the scope and visibility of variables in c plus plus function?

If you define a variable inside of your function, the variable can be referred and used only inside of that function. It means that you will not able to use the variable in another function (including main). Area of code where your variable can be used after declaration is usually called visibility of the variable.


Why do you member functions defined outside the class?

A member function of a class can be defined outside the class using scope resolution :: operator Thanks Rajneesh


Which operand should be passed in the binary overloaded operator function as a second operand?

The right-hand operand; the r-value of the operator. Unary operators have one operand while tertiary operators have three operands. All binary operators have two operands, the l-value and the r-value. The l-value is the operand to the left of the operator while the r-value is the operand to the right of the operator. Thus, in the expression x + y, x is the l-value while y is the r-value. When overloading binary operators in a class, you need only specify the r-value. The l-value is the instance of the class to which the operator applies and therefore does not need to be specified. For instance: class MyClass { public: MyClass(int data=0):m_data(data){} // default constructor int operator+ (const int rhs) const {return(m_data+rhs);} private: int m_data; }; While this allows you to return the sum of your class instance and an integer, it does not allow you to return the sum of an integer and an instance of your class. For example: MyClass obj(5); int x = 10; int y = obj + x; // OK! y is 15. int z = x + obj; // Compiler error! No operator exists that accepts an r-value of type MyClass. To fix this error and allow for two-way addition, you must declare a binary operator overload outside of the class. You cannot do it inside the class because the l-value is an int, not an instance of MyClass. The external overload requires two parameters, the l-value and the r-value of the operator: int operator+(const int lhs,const MyClass&amp; rhs) {return(rhs+lhs);} Note that the implementation simply reverses the operands. This is functionally equivalent to making the following explicit call: return(rhs.operator+(lhs)); Note also that since MyClass::operator+ is a public operator of MyClass, this overload does not need to be declared a friend of MyClass (a common misconception). However, the overload must be declared in the same file where the class is declared since it is only of relevance to MyClass but should be made available wherever MyClass is accessible.

Related questions

What is the correct declaration of the assignment operator overloaded function inside the travel class?

class travel {...travel &operator=( some type );... or ...travel operator=( some type );...};


What is local declaration?

Declarations inside a function is called local declaration


What is meant when you say you overload an operator?

All operators are built-in but not all operators can operate upon data types they know absolutely nothing about. There are some exceptions such as the new operator and the sizeof operator -- both will work on any datatype. However, for those that cannot, operator overloads allow you to cater specifically for those types. An operator overload is implemented just as you would overload a function, but it is not a function per se because operators have different calling conventions to functions. It is also important to keep in mind that just because you can overload an operator, it does not mean that you should. Operators should only be overloaded when the overload would allow a user to interact with your data type in an intuitive manner; a manner that is consistent with the operator's intended purpose. So while it can be amusing to overload the plus (+) operator to perform a subtraction (-), it could hardly be called intuitive. The assignment operator is the most overloaded operator of them all. This is because it is extremely useful to be able to copy the members of one object and assign those values to another object of the same type. We can also overload the assignment operator to cater for objects of different types, but such assignments are rarely intuitive. Does it make sense to assign the properties of a banana object to a person object? Probably not. Even if you could find a practical reason for doing so, would it be an intuitive operation? Definitely not. Therefore there's no point in providing an operator to cater for this. To create an operator overload, the declaration will often be placed inside the class it pertains to. However there are exceptions. The output stream insertion operator is a good example of this. The following example demonstrates how we can overload an internal operator (the assignment operator) as well as an external operator (output stream insertion operator). #include&lt;iostream&gt; // required to make use of I/O streams class A { private: unsigned m_data; public: // constructors... A (const unsigned data = 0): m_data (data) {} A (const A&amp; copy): m_data (copy.m_data) {} // accessor function (interface) unsigned get_data() const { return m_data; } // operator overloads... A&amp; operator= (const A&amp; rhs) { m_data = rhs.m_data; } A&amp; operator= (const unsigned rhs) { m_data = rhs; } }; std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const A&amp; a { os &lt;&lt; a.get_data(); return os; } int main() { A a, b; // invoke default constructors a = 42; // call assignment operator overload b = a; // call default assignment operator overload // call insertion operator overload std::cout &lt;&lt; a &lt;&lt; std::endl; std::cout &lt;&lt; b &lt;&lt; std::endl; } Output: 42 42


What is a global declaration?

A global declaration of a function or variable is a declaration that is at the global scope of the program, not inside another function. This means that the name will be visible within all functions in the program.


What the operator used when invoking a function?

parenthesis - () are used along with the function name to invoke a function.Eg:Consider a function named trial. Then the invoking statement would be:trial();If the function has arguments, then these arguments are passed inside the parameters.


When determining whether a number is inside a range which logical operator is best to use?

And operator


How do you make a variables from outside a function work in that function?

declaring a variable before main() will be accessed in main as well as in function which is called a "global variable" or "external variable". *edit* a very good programming practice, which is also necessary with some compilers, is to declare the variable inside the function as well with the extern declaration before the type declaration. for example if you had this inside a function: int func(){ int x = 0; x++; return x; } that would be fine, but to declare it externally, you should include extern inside the function int x = 0; int func(){ extern x; x++; return x; } assuming that we declared the variable globally, which we did.


Explain the scope and visibility of variables in c plus plus function?

If you define a variable inside of your function, the variable can be referred and used only inside of that function. It means that you will not able to use the variable in another function (including main). Area of code where your variable can be used after declaration is usually called visibility of the variable.


Why do you member functions defined outside the class?

A member function of a class can be defined outside the class using scope resolution :: operator Thanks Rajneesh


Which operand should be passed in the binary overloaded operator function as a second operand?

The right-hand operand; the r-value of the operator. Unary operators have one operand while tertiary operators have three operands. All binary operators have two operands, the l-value and the r-value. The l-value is the operand to the left of the operator while the r-value is the operand to the right of the operator. Thus, in the expression x + y, x is the l-value while y is the r-value. When overloading binary operators in a class, you need only specify the r-value. The l-value is the instance of the class to which the operator applies and therefore does not need to be specified. For instance: class MyClass { public: MyClass(int data=0):m_data(data){} // default constructor int operator+ (const int rhs) const {return(m_data+rhs);} private: int m_data; }; While this allows you to return the sum of your class instance and an integer, it does not allow you to return the sum of an integer and an instance of your class. For example: MyClass obj(5); int x = 10; int y = obj + x; // OK! y is 15. int z = x + obj; // Compiler error! No operator exists that accepts an r-value of type MyClass. To fix this error and allow for two-way addition, you must declare a binary operator overload outside of the class. You cannot do it inside the class because the l-value is an int, not an instance of MyClass. The external overload requires two parameters, the l-value and the r-value of the operator: int operator+(const int lhs,const MyClass&amp; rhs) {return(rhs+lhs);} Note that the implementation simply reverses the operands. This is functionally equivalent to making the following explicit call: return(rhs.operator+(lhs)); Note also that since MyClass::operator+ is a public operator of MyClass, this overload does not need to be declared a friend of MyClass (a common misconception). However, the overload must be declared in the same file where the class is declared since it is only of relevance to MyClass but should be made available wherever MyClass is accessible.


How do you roll a tractor tire inside out?

A clever backhoe operator is capable of doing it.


Where would a balance not function correctly?

it will not function inside of an anus.