Some mind teasers

Discussion in 'C' started by shabbir, Nov 18, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I will put some mind teasers of C++ in this thread and if you have just go on adding them.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    We have

    const A& operator=(const A& obSrc)
    and
    A& operator=(const A& obSrc)

    What is the difference?
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Are there any reasons one should avoid using static variables in abstract base classes?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    How will you call a C++ function from C and Vice Versa?
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Hi.

    Code Sample
    Code:
    #include <iostream>
    using namespace std;
    class A
    {
    public:
        A() { throw 0; }
    
    };
    
    int main()
    {
        try
        {
            A *pA = new A;
            delete pA;
        }
        catch (...)
        {
        }
    }
    Is this good code and why?

    Constructor throws an exception. Now will the destructor be called ?

    When is the memory allocated when a new is called? Is it after the constructor is executed or just when the constructor starts?

    What happens with memory allocated for the object itself?
    Who's responsible for deallocating it in such a case?
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Is it possible to catch an assertion error?

    assert (false);
    catch (...) {}
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Is it possible to implement a Singleton without using static variables or global variables.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Why constructors do not have return values?
    What if I want to check whether the object is successfully created. We could have checked that by returning some value and finding it.
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Write a simple template program where the .h files contains the class definition and .cpp files contains the class declaration.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    List the operators that cannot be overloaded normally as well as friend functions?
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What is overloading, overwriting, and overriding?
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What you mean by virtual destructor and what are the advantages of them??
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I always understood that in C++, if I said

    a + b

    a.operator+(b) is called.

    Now this makes sense with the operator<< when used in the following way

    cout << 100; // converts to cout.operator<<(100);

    but then how does one explain this one

    cout << end; // endl(cout).

    In what order does C++ look for operator functions? What all functions does it search before giving up? In other words

    when C++ sees

    a+b

    does it look for the existence of :

    a.operator(b)
    b(a)
    a(b) ....???

    or is the operator<< a special case?
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Is there any function(s) which cannot be overloaded?
     
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Should a Singleton class have a destructor and if it has what is the purpose of it?
     
  16. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    Who do you expect to post the answers? or is it just the questions thread?
     
  17. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I had the plans of putting the questions only but if you wish to post the answers quote the message and put them as a post to the thread as well.

    There is always an option of searching within the thread so that wont be a probs.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice