Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor).
When programmer is overloading = operator using friend function, two = operations will exists:
1) compiler is providing = operator
2) programmer is providing(overloading) = operator by friend function.
Then simply ambiguity will be created and compiler will gives error. Its compilation error.
Chat with our AI personalities
obj1 = obj2;
Operator "=" can be overloaded either by the member function or by the friend operator function.
In case of the friend function, the obj1 and obj2 are should be from the different classes. If they are from the same class, the compilation error will occur. We can't use the "this" pointer in the friend function since it is not the member function of the class.
So,
If both objects are from the same class it is better to for operator function (operator=) as member function.
If the objects are from the different class, then the operator function as friend function is advisable.
Because there's no way to designate the left-hand operand of an assignment via a friend function. The left-hand operand is a given when assignment is implemented as a member function.
* Assignment operator : =
* Function call operator : ()
* Subscripting operator : []
* Class member access operator.
You cannot overload the sizeof() operator because that could introduce uncertainty in its evaluation. The sizeof() operator must always produce an accurate and logically predictable result, thus all user-intervention is completely forbidden.
The only reason to overload a template function is to provide an overload that differs in the number and type of arguments that cannot be addressed by the template function alone. You simply declare them as you would any other overload. The overloads may themselves be template functions, but there must be no ambiguity: every template function must generate an unique function signature. Remember that template functions generate overloads at compile time on an as-required basis.
it cannot be operator overloaded.
I'm not sure I fully understand the question. Operators and functions are not the same. However, many operators also have named alternatives implemented as functions, such that "add" is synonymous with the binary increment operator while "plus" is synonymous with the unary plus operator. However these are completely separate implementations that do the same thing such that the function implementation typically invokes the operator (with implicit inline expansion to factor away the unwanted function call). There is no way to determine the underlying function of an operator, nor the underlying operator of a function without having access to the implementation source code. Such implementation details cannot be accessed at compile time let alone runtime.
Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...