Sponsored Links

Interview Questions
Recommended Books












INTERVIEW QUESTIONS C++ OPERATORS IN C++ DETAILS
Question :

Why can't we overload the sizeof, :?, :: ., .* operators in c++?



Category Operators in C++ Interview Questions
Rating (3.5) By 97 users
Added on 8/18/2011
Views 6044
Rate it!

Highly Recommended, Hand Picked Operators in C++ Books



Question :

Why can't we overload the sizeof, :?, :: ., .* operators in c++?


Answer:

Source: CoolInterview.com

The restriction is for safety.For example if we overload . operator then we cant access member in normal way for that we have to use ->.
Source: CoolInterview.com



Source: CoolInterview.com

I think the above answer is not appropriate .<br><br>According to me all these operators use name instead of operand ,so we can`t pass any name (either of variable,class) to any function . We must have to pass the operand for that .
Source: CoolInterview.com



Posted by: Ankur Bamby

Contact Ankur Bamby Contact Ankur Bamby

Source: CoolInterview.com

Nice answer, but it does not suite for :? operator as it does not take name as parameter.<br>BTW, the reason we cannot overload :? is that it takes 3 argument rather than 2 or 1. There is no mechanism available by which we can pass 3 parameter during operator overloading.<br>For other operators, the previous ans is enough.
Source: CoolInterview.com



Posted by: Tapesh Maheshwari

Contact Tapesh Maheshwari Contact Tapesh Maheshwari

Source: CoolInterview.com

In any languages all operator has some precedence, due to precedence they work.like +,-,() all have some precedence, as we know operator overloading work differently without changing the specific meaning of the operator and in this case :,?:, etc. have no predefined precedence.And if we want to overload these operator then compiler does not understand and conflicts, and generates an error.
Source: CoolInterview.com



Posted by: jitendra singh

Contact jitendra singh Contact jitendra singh

Source: CoolInterview.com

According to Bjarne Stroustrup, the operators ., .* ,:?, :: & sizeof() cannot be overloaded as they take name as their argument whereas all other operators take value as argument.<br><br>Class A<br>{<br>public:<br> int i;<br>};<br><br>A a,b,c;<br><br>Eg: c = a+b - both a & b actually refer to some memory location, so "+" operator can be overloaded, but the "." operator, like a.i actually refers to the name of the variable from whom the memory location has to be resolved at time and thus it cannot be overloaded.<br><br><br>
Source: CoolInterview.com



Posted by: Sunil Paduchuru

Contact Sunil Paduchuru Contact Sunil Paduchuru

Source: CoolInterview.com

Most operators can be overloaded by a programmer. The exceptions are<br><br> . (dot) :: ?: sizeof<br><br>There is no fundamental reason to disallow overloading of ?:. I just didn't see the need to introduce the special case of overloading a ternary operator. Note that a function overloading expr1?expr2:expr3 would not be able to guarantee that only one of expr2 and expr3 was executed.<br><br>Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it. Consider:<br><br> X a[10];<br> X* p = &a[3];<br> X* q = &a[3];<br> p++; // p points to a[4]<br> // thus the integer value of p must be<br> // sizeof(X) larger than the integer value of q<br><br>Thus, sizeof(X) could not be given a new and different meaning by the programmer without violating basic language rules.<br><br>In N::m neither N nor m are expressions with values; N and m are names known to the compiler and :: performs a (compile time) scope resolution rather than an expression evaluation. One could imagine allowing overloading of x::y where x is an object rather than a namespace or a class, but that would - contrary to first appearances - involve introducing new syntax (to allow expr::expr). It is not obvious what benefits such a complication would bring.<br><br>Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by . For example:<br><br> class Y {<br> public:<br> void f();<br> // ...<br> };<br><br> class X { // assume that you can overload .<br> Y* p;<br> Y& operator.() { return *p; }<br> void f();<br> // ...<br> };<br><br> void g(X& x)<br> {<br> x.f(); // X::f or Y::f or error?<br> }<br><br>This problem can be solved in several ways. At the time of standardization, it was not obvious which way would be best. For more details, see D&E.
Source: CoolInterview.com



Posted by: Hitesh

Contact Hitesh Contact Hitesh

Source: CoolInterview.com

these type of operator don't there own value,but in overloading not change there own value while overloading that's why if there don't have any value how this is possible to overload..
Source: CoolInterview.com



Posted by: pratik arya

Contact pratik arya Contact pratik arya

Source: CoolInterview.com

these operators are not used in other meanigful restriction<br>for example + used in addition and sign that operators are used in<br>numerical calculation depends on precedence
Source: CoolInterview.com



Posted by: pon babitha

Contact pon babitha Contact pon babitha

Source: CoolInterview.com

(.)dot operators cann't use in operator overloading because dot is predifined for software programe,with out dot we cann't acess namespase,calling of an object,inheretance.so if we use as overloading it may confuse to the software programe
Source: CoolInterview.com



Posted by: sridhar

Contact sridhar Contact sridhar

If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification Enter the above shown code: *
Inform me about updated answers to this question

Related Questions
View Answer
What is a scope resolution operator?


View Answer
Can you explain the term "resource acquisition is initialization?"


- Operators in C++ Interview Questions & Answers"> View Answer
What is the difference between operator new and the new operator?

View Answer
Difference between a "assignment operator" and a "copy constructor"


- Operators in C++ Interview Questions & Answers"> View Answer
Define copy constructor? What is the use of copy constructor?

View Answer
In C++, what is a constructor,destructor?
View Answer
Can you declare the override method static while the original method is non-static?
View Answer
How can you overload a method?
View Answer
What is a scope resolution operator?
View Answer
Difference between realloc() and free()?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View ALL Operators in C++ Interview Questions & Answers



User Options
India News Network

Latest 20 Questions
What are the Hadoop related Projects at Apache?
What are the modules included in Apache Hadoop Project?
What is Apache Hadoop?
What is a Private Cloud? Do we need one to handle Big Data?
How Can We Cope with the Impact of Big Data?
What skills do Big Data analysts need to have?
What does a Big Data analyst do?
What are some real-world examples of Big Data and how it is used?
What is Big Data Analytics?
Why Big Data is becoming so important?
What is the impact of Big Data on Sports Industry?
What is the impact of Big Data on Real estate Industry?
What is the impact of Big Data on Retail Banking Industry?
What is the impact of Big Data on Retail Industry?
What is the impact of Big Data on Technology Industry?
What is the impact of Big Data on Information Technology Industry?
What is the impact of Big Data on Internet of Things (IoT) Industry?
What is the impact of Big Data on Media Industry?
What is the impact of Big Data on Education Industry?
What is the impact of Big Data on Healthcare Industry?



Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |




Copyright ©2003-2016 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions



Cache = 0.015625 Seconds