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

Thread: Online C++ Test Questions

Threaded View

  1. #16
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,548

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center