CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 28

Threaded View

  1. #16
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Online C++ Test Questions

    Quote Originally Posted by gulHK View Post
    Hello everyone

    Could anyone please help me with this online interview question?

    Code:
    Object1 and Object2 are the names of two different classes.
    The Class Object2 has one Object and the Class Object1 does not have any objects
     
    Object2 obj2 = new Object2();
    cout << dynamic_cast< Object1>(obj2) << endl; ....... what does this line print?
    Many Thanks
    As previously discussed in this thread, the statement

    Code:
     Object2 obj2 = new Object2();
    is valid if and only if Object2 has a constructor that takes a Object2* as parameter and that constructor isn't declared as explicit.

    If the Object1 and Object2 classes are distinct with no relationship between them, then even assuming the obj2 assignment is correct, I doubt the dynamic_cast statement will compile and I would expect a compile error along the lines that Object2 is not polymorphic.

    IMO whos ever setting these interview questions are not doing a very good job of producing understandable c++ code in the absence of class definitions! If I was given this code I would be saying that it doesn't compile - but I suspect the answer they are looking for is null (null pointer) as the dynamic_cast will fail.

    See
    http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
    Last edited by 2kaud; September 21st, 2013 at 07:10 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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