CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jun 2003
    Posts
    188

    C++ Inverview question

    The recuriter tells me what questions the cleint will ask in the phone interview, but he cannot elaborate on what it means.

    Here is the question...

    Name 4 cases on using copy method of a class?


    What is meant by this?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Inverview question

    It means that the person being asked to provide the answer should have a good understanding

    If someone just provided the answer then that would just be cheating the prospective employer
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ Inverview question

    You mean the copy constructor? It's called

    1. when initializing an object using another object of the same type,
    2. when passing objects to functions by value, and
    3. when returning objects from functions by value.

    Is there a fourth case? Maybe it's when it's NOT used (but one would expect it to be) and that's in an assignment.

  4. #4
    Join Date
    Jun 2003
    Posts
    188

    Re: C++ Inverview question

    Yes, I think he is talking about the copy constructor. The forth may be the difference between a shallow and deep copy.

    Or maybe when you do this...

    CObj a;
    CObj b;

    b = a;

    And overload the = operator
    Last edited by mburke; October 2nd, 2004 at 11:56 PM.

  5. #5
    Join Date
    Jun 2003
    Posts
    188

    Re: C++ Inverview question

    Quote Originally Posted by TheCPUWizard
    It means that the person being asked to provide the answer should have a good understanding

    If someone just provided the answer then that would just be cheating the prospective employer

    Not really, I am a very good programmer. They will get their money's worth if they hire me. Interviewing is a stupid game. I hate dog and pony shows. I just needed some clarification on what this question is looking for.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ Inverview question

    Quote Originally Posted by mburke
    Yes, I think he is talking about the copy constructor. The forth may be the difference between a shallow and deep copy.

    Or maybe when you do this...

    CObj a;
    CObj b;

    b = a;

    And overload the = operator
    Basically when your class is so complex you need a deep copy that's when you must provide your own copy constructor.

    Assignments doesn't involve the copy constructor. In fact only initialization does (in the three case I've already mentioned).

    The only fourth case I can think of is when objects are stored in STL containers. All objects are copied in and out of containers using the copy constructor (and sometimes even within).

    Those are the 4 cases I would mention. Good luck!

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Inverview question

    Basically when your class is so complex you need a deep copy that's when you must provide your own copy constructor.
    Very simple classes need a copy sonstructor to function properly...

    Code:
    class P
    {
       public:
         P() { c = NULL; }
         ~P() { delete c; }
    
         char *c;
    };
    If you do noit have a copy constructor (and assignment operator) for this class, it will most likely not function:
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Jun 2003
    Posts
    188

    Re: C++ Inverview question

    Quote Originally Posted by _uj
    Basically when your class is so complex you need a deep copy that's when you must provide your own copy constructor.

    Assignments doesn't involve the copy constructor. In fact only initialization does (in the three case I've already mentioned).

    The only fourth case I can think of is when objects are stored in STL containers. All objects are copied in and out of containers using the copy constructor (and sometimes even within).

    Those are the 4 cases I would mention. Good luck!
    Yes, your right. What I have will invoke the = operator, which can be overloaded.

    This is what I meant. It will invoke the copy constructor...

    CObj a;
    CObj b = a;

    ---
    Also, C++ does provide a default copy constructor if you don't.

  9. #9
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ Inverview question

    Quote Originally Posted by TheCPUWizard
    Very simple classes need a copy sonstructor to function properly...
    The simple example class you provided has a hidden complexity, namely an object allocation on the heap. To make such a class "copyable" a deep copy must be made and that signals that you must provide your own copy constructor.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ Inverview question

    Quote Originally Posted by mburke
    This is what I meant. It will invoke the copy constructor...

    CObj a;
    CObj b = a;
    I'm quite new to C++ so I wouldn't bet on it but from what I understand, in the above case, the copy constructor is NOT invoked. The assignment operator is called and if it's the default one it just copies the class members one by one shallowly from the a to the b object.

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Inverview question

    mburke is correct!

    Although a definition with an "=" sign in it LOOKS like an assignment, it is the creation of a new object and therefor uses a constructor

    The alternative would be to construct a new object using a default constructor and then invoke the assignment operator. This would (potentially) result in a significant amount of wasted work when the copy constructor can accomplish the task in one "simple" function.

    Note: This is a common source of mis-understanding and bugs!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ Inverview question

    Quote Originally Posted by TheCPUWizard
    mburke is correct!

    Although a definition with an "=" sign in it LOOKS like an assignment, it is the creation of a new object and therefor uses a constructor
    I'm not quite convinced. Say we have this,

    CObj a; // case 1
    CObj b = a;

    In my view the above ought to be equivalent to,

    CObj a; // case 2
    CObj b;
    b = a;

    and NOT to

    CObj a; // case 3
    CObj b(a);


    In the first two cases a and b are constructed using the default constructor. Then a is copied to b using the assignment operator. No copy constructor is ever called.

    In the third case a is default constructed and then b is copy constructed using a.

    Well, I'm probably wrong but there's no harm in asking I hope.

  13. #13
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Inverview question

    Sorry _uj...

    While it might LOOK that way, the specification [section 8.5.2] explicitly states otherwise.

    Code:
    class obj1;
    clase obj2(obj1);
    class obj2 = obj1;  // Equivilant to previous line...
    You can even step through....

    Code:
    class A
    {
    public:
    	A() { x = 0; }
    	A(const A &b) { x = 1; } 
    	A const & operator =(A const & b) { x = 2; return *this; }
    	int x;
    };
    
    void f()
    {
    	A a1;
    	A a2 = a1;
    	A a3(a1);
    }
    Just out of curiousity, have you ever thought to test your ideas? or to read reference material?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  14. #14
    Join Date
    Nov 2003
    Posts
    1,405

    Re: C++ Inverview question

    Quote Originally Posted by TheCPUWizard
    Just out of curiousity, have you ever thought to test your ideas? or to read reference material?
    I very much do but you'll have to consider the more obscure points when they show up otherwise you'll be reading the manual all day instead of programming. You'll have to live with the fact that sometimes you detect that you've been writing correct code but it's been working for the wrong reason. When I look at this,

    CObj b = a;

    I still see a compund statement. First a declaration of b and then an assignment of a, but now I've learned it's actually treated as a special construction case.

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ Inverview question

    I very much do but you'll have to consider the more obscure points when they show up otherwise you'll be reading the manual all day instead of programming.
    That is why I listed "check reference materials" after "test your ideas".

    I do not think it unreasonable for a person to check an idea after TWO people point out the proper answer...

    In any case, enough said, lets go find/solve some new issues.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured