DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: tricky interview question

  1. #1
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604

    tricky interview question

    Hi everybody,

    last Friday an interviewer had one on me. Although I think I have sussed
    the answer out since then, I was frankly stunned at the time. The
    (inoccuous) question was:
    What is the difference between CMyClass x; and CMyClass x()?

    My answer was wholly inappropriate, but the question bugged me so
    much that I created the following program

    Code:
    class X
    {
        static int x;
        int xx;
    public:
        X(int i = 0)
        {
            xx = x;
            printf("X%d\n",xx);
            x++;
        }
        ~X()
        {
            printf("~X%d\n",xx);
        }
        void show()
        {
            printf("-- X%d\n",xx);
        }
    };
    int X::x = 0;
    
    int main(int argc, char* argv[])
    {
    // (Line 1 and 2 being my test, the rest is just "garnish".)
    
    /* 1 */    X x0;   // <---
    /* 2 */    X x1(); // <--- 
    /* 3 */    X* x2 = new X;
    /* 4 */    X* x3 = new X();
    /* 5 */    X x4(1);
    /* 6 */    X* x5 = new X(5);
    
    /* 7 */    delete x5;
    /* 8 */    delete x3;
    /* 9 */    delete x2;
    
    	return 0;
    }
    And sure enough line 2 doesn't create an object! So although it looks like
    lines 4/5, where the constructor X(int i=0) is called, in line two
    the same constructor is *NOT* called.

    Really got me...

    I don't want to spoil the fun for anyone just yet, so I won't give my
    (revised) answer in the moment, but it's a "forehead slapper"...

    Cheers,

    Dieter
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  2. #2
    Join Date
    Feb 2004
    Location
    Indianapolis, IN
    Posts
    433
    Quote Originally Posted by drkybelk
    I won't give my
    (revised) answer in the moment, but it's a "forehead slapper"...
    This is one where you may have been thinking too hard because it was an interview. ;) Unless I'm missing something, if you step back and keep things simple, then the answer should be easy...

    I'll send you my answer in a private message. I'll let others ponder it here for a while :D

    Brad!
    -------------------------------------------------
    Bradley L. Jones
    Author, and more.
    QuinStreet Enterprise Network / Developer.com Network
    (Developer.com, CodeGuru.com, DevX, jGuru, and more!)

    -------------------------------------------------

  3. #3
    Join Date
    Feb 2004
    Location
    Indianapolis, IN
    Posts
    433
    You have PMs turned off, so I'll give the simple answer here...

    CMyClass x;
    creates an object called x of type CMyClass

    CMyClass x()?
    is a method called x that has a return type of CMyClass
    -------------------------------------------------
    Bradley L. Jones
    Author, and more.
    QuinStreet Enterprise Network / Developer.com Network
    (Developer.com, CodeGuru.com, DevX, jGuru, and more!)

    -------------------------------------------------

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi Brad,

    actually you're on the right track, I s'pose, but CMyClass x() is *NOT* a function call...

    ;-)

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    ATTN: SPOILER AHEAD
    Read on if you want to know the answer.

    Code:
    CMyClass x();
    is always a function declaration which declared a function called x that takes no arguments and returns CMyClass by value.

    In a slightly different context (also given in your question), the syntactic difference changes the semantics too:

    Code:
    CMyClass * p= new CMyClass x();
    CMyClass *p =new CMyClass x;
    Here, the first line forces default initialization whereas the latter doesn't, but this difference is meaningful *only if* MyClass is a POD type (e.g., a struct, union etc. that has no constructor(s), destructors and so on). Otherwise, there's absolutly no difference -- in either case the default ctor is called.

    BTW, this topic is discussed in one of my C++ tips. Conclusion: go through as many vintage tips as you can before a job interview!
    Last edited by Danny; 06-26-2006 at 02:32 PM.
    Danny Kalev

  6. #6
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Cheers Danny,

    I do go through the tips regularly and I can only agree. They are a good preparation.
    But I couldn't take a step back far enough to see the obvious...
    One never stops learning.

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  7. #7
    Join Date
    Oct 2005
    Posts
    173
    a default constructor is ONLY created when no constructor is defined right?
    given his class, why isn't X x0; an error?

    so the constructor in Dieter's example above is treated as a default constructor?
    X(int i = 0)


    so if you have a class with only one constructor but not default value:
    X(int i);

    can you still do this: X x0;
    the missing parameter would not be an error?

    X(int i = 0)
    X(int i = 0, double j = 1.5)
    is it legal to have the 2 constuctors at the same time?
    if so, how would the compiler know which to use?
    Last edited by rssmps; 06-27-2006 at 05:42 PM.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    No, a default constructor is a constructor that can be called with no arguments. It can be a user-written ctor ot a compiler generated one. In the original example, the constructor is declared as:
    Code:
    X(int i = 0);
    Which means that it can be called with no arguments, hence, it's a default ctor.

    If the declaration didn't have a default value, i.e.,
    Code:
    X(int i);
    Code:
    X x0;
    wouldn't compile because the class wouldn't have a matching ctor.

    Finally, two constructors that takes exactly the same parameters are of course an error:
    Code:
    X(int i = 0)
    X(int i = 0, double j = 1.5)
    Here, the compiler can't tell whether
    Code:
    X x(1);
    uses the first ctor or the second one. The same is true for
    Code:
    X x;
    However, programmers (hopefully) don't write such code. Instead, they use a single ctor with multiple parameters that have default values:
    Code:
    X(int i = 0, double j = 1.5);
    This way you can create X objects like this:
    Code:
    X x;
    X x1(1);
    X x2(1, 9.0);
    When these objects are contructed they call the same constructor. And yes, this constructor is also a default constructor.

    BTW, you want to make a constructor that may be called with a single argument an explicit constructor:
    Code:
    explicit X(int i = 0, double j = 1.5);
    Last edited by Danny; 06-28-2006 at 04:22 AM.
    Danny Kalev

  9. #9
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,

    If you default all arguments in a constructor, this constructor will be your "default constructor". Any attempt to add another constructor without arguments or with all arguments defaulted will result in a compilation error.
    Obviously you can have any number of constructors (I don't know whether there is any physical limitation in count, but I certainly never reached the limit should it exist).
    Just make sure that the signatures are "different enough" from each other.
    So it would be legal for example to have the following constructors:
    Code:
    X(int i = 0);
    X(double f, int i = 1);
    X(const CMyClass& mc);
    Problematic would be:
    Code:
    X(int i = 0);                     // default constructor
    X(int i = 0, double j = 1.5); // another default constructor - AMBIGUOUS
    X(long i);                         // ambiguous, because parameter types 
                                         // too similar with first constructor
    You get the drift...

    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  10. #10
    Join Date
    Oct 2005
    Posts
    173
    Quote Originally Posted by Danny
    No, a default constructor is a constructor that can be called with no arguments....Which means that it can be called with no arguments, hence, it's a default ctor.
    Ah...this explains it. I had interpreted the ctor with default values as not a default ctor.

    Thanks!

Similar Threads

  1. interview question
    By rainerblue in forum Java
    Replies: 3
    Last Post: 03-25-2006, 06:11 PM
  2. Replies: 2
    Last Post: 02-01-2006, 02:34 PM
  3. Display one record at a time!
    By ASP learner in forum ASP.NET
    Replies: 5
    Last Post: 10-08-2002, 07:17 PM
  4. question on best programming option
    By James in forum .NET
    Replies: 2
    Last Post: 03-28-2002, 08:14 AM
  5. tricky question
    By kaz in forum Database
    Replies: 5
    Last Post: 09-24-2001, 12:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center