CoolInterview.com - World's Largest Collection of Interview Questions
Start Your Own website Now
Sponsored Links

Interview Questions
Our Services

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!



    Send your Resume to 6000 Companies


  • 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 (0.4) By 1961 users
    Added on 8/18/2006
    Views 7682
    Rate it!
    Answers:

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



    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 .



    Posted by: Ankur Bamby    

    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.



    Posted by: Tapesh Maheshwari    

    Contact Tapesh Maheshwari Contact Tapesh Maheshwari

    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.



    Posted by: jitendra singh    

    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>



    Posted by: Sunil Paduchuru    

    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.



    Posted by: Hitesh    

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



    Posted by: pratik arya    

    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



    Posted by: pon babitha    

    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,inheretance.so if we use as overloading it may confuse to the software programe



    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?"

    View Answer
    What is the difference between operator new and the new operator?

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

    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

    User Options
    Sponsored Links


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

    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Dedicated Servers | C Interview Questions & Answers

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


    Cache = 0.03125 Seconds