- World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.

Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

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

Answer: 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 ->.

Category Operators in C++ Interview Questions & Answers - Exam Mode / Learning Mode
Rating (3.5) By 97 users
Added on 8/18/2011
Views 6092
Rate it!
Question: Why can't we overload the sizeof, :?, :: ., .* operators in c++?


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:

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:

Answered by: Ankur Bamby | Date: | Contact Ankur Bamby Contact Ankur Bamby

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:

Answered by: Tapesh Maheshwari | Date: | Contact Tapesh Maheshwari Contact Tapesh Maheshwari

In any languages all operator has some precedence, due to precedence they +,-,() 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:

Answered by: jitendra singh | Date: 1/26/2008 | Contact jitendra singh Contact jitendra singh

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:

Answered by: Sunil Paduchuru | Date: 12/30/2009 | Contact Sunil Paduchuru Contact Sunil Paduchuru

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:

Answered by: Hitesh | Date: 3/7/2010 | Contact Hitesh Contact Hitesh

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:

Answered by: pratik arya | Date: 4/25/2010 | Contact pratik arya Contact pratik arya

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:

Answered by: pon babitha | Date: 6/1/2010 | Contact pon babitha Contact pon babitha

(.)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, if we use as overloading it may confuse to the software programe Source:

Answered by: sridhar | Date: 6/25/2010 | 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

  • 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.

Post your answer here

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" class="btn btn-primary btn-xs">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" class="btn btn-primary btn-xs">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 - Best Freshers & Experienced Jobs Website.

View All Operators in C++ Interview Questions & Answers - Exam Mode / Learning Mode

India News Network
Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
Cache = 0.03125 Seconds