Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6711695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:10:17+00:00 2026-05-26T08:10:17+00:00

Consider I am creating objects of two classes: BaseClass B; DerievedClass D; and then

  • 0

Consider I am creating objects of two classes:

BaseClass B;
DerievedClass D;

and then i am doing:

B=D;
D=B;

Which is legal and why. This was a question asked by an interviewer of a C++ related Job. I know mostly B=D; will be valid (object slicing); but is it so that D=B; will only be valid if B has a default constructor? Yes, then why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:10:17+00:00Added an answer on May 26, 2026 at 8:10 am
    B=D;
    D=B;
    

    First line will always compile. The second line may or may not compile, as it entirely depends on how you have written each class. The rest of the answer will shed light on it. Read on.


    is it so that D=B; will only be valid if B has a default constructor?

    No.

    D=B will be valid only if D has defined operator= which takes B as argument.

    Derived & operator=(const Base &base); //member of Derived
    
    Base B;
    Derived D;
    D = B;     //allowed - assignment
    

    Or if you do that in the initialization of D, then it will valid only if a constructor of D takes B as argument.

    Derived(const Base &base); //a constructor of Derived
    
    Base B;
    Derived D = B; //allowed - initialization
    

    Or B has defined a user-defined conversion to D.

    operator Derived(); //member of Base
    
    Base B;
    Derived D = B; //allowed      - initialization
    D = B;         //also allowed - assignment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this problem: I have a program which should fetch (let's say) 100 records
Consider this code: // Creating an empty object, without inheriting (binding) to any prototype
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider these classes. class Base { ... }; class Derived : public Base {
I'm starting to consider creating a class library that I want to make generic
I have a general question regarding the syntax of throwing an object. Consider: #include
I have certain objects in my domain which are not aggregate roots/entities, yet I
In an answer to Is it safe to store objects of a class which
Please consider the following scenario, There are two web applications App1 & App2. A
I am going through Effective Java and some of my things which I consider

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.