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 8047509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:07:26+00:00 2026-06-05T06:07:26+00:00

I have the following situation class B { public: B() {}; virtual ~B() {};

  • 0

I have the following situation

class B {
public:
    B() {};
    virtual ~B() {};
    virtual void seti( int x ) { i = x; };
    virtual void setj( int x ) { j = x; };
    virtual void add() =0;

protected:
    int i;
    int j;
};
class D :  public B {
public:
 virtual void add() { cout << "D-add:" << i + j << endl; }; 
};

class E: public B {
public:
    void seti( int x ) { i = x; };
    void add() { cout << "E-add:" << i + j << endl; }; 
    void mult() { cout << "E-mult:" << i * j << endl; }; 
};

int _tmain(int argc, _TCHAR* argv[])
{
    D *d = new D();
    d->seti(4); d->setj(5); d->add();
    E*e = d;
    e->seti(8); e->add(); e->mult();
    return 0;
}
I get the following error
1>.\CallBack.cpp(38) : error C2440: 'initializing' : cannot convert from 'D *' to 'E *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-styl`enter code here`e cast

What i want to do is when I instantiate E, I was use all the information / members of D and do some thing more with it. Should I use hierarchical inheritance like above or should I use multi-level inheritance or is there any other better way. Please advise. Thank you !

  • 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-06-05T06:07:28+00:00Added an answer on June 5, 2026 at 6:07 am

    I think what you want is a “conversion ctor” – a constructor on E which takes a reference/pointer to D as the parameter and populates itself basing on D’s values. To copy fields defined on B, you can do this:

    class B {
    public:
    ....
      B(B& source) { this->i = source.i; this->j = source.j; }
    ....
    }
    class E {
    public:
    ....
      E(D& source) : B(source) { // no need to do anything about i,j
      }
    ....
    }
    

    Also, you can write a converter class (DtoEConverter) which will have a function to do that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have the following situation #include <iostream> class Animal { public: virtual void
I have following situation class base { public: virtual void Accepted(SOCKET s) //// Event
I have the following situation: typedef void (*F_POINTER)(ClassName*); class ClassName { public: F_POINTER f;
I have the following class definition: class IRenderable { public: virtual void Render( wxBitmap
I have the following situation: class A { public: A(int whichFoo); int foo1(); int
Imagine I have the following situation: Test1.java import java.lang.ref.WeakReference; public class Test1 { public
I have the following situation: public abstract class A { private Object superMember; public
Assume the following schema class Person{ public int Id {get;set;} public virtual ICollection<Province> Provinces
I have a problem with JPA 1.0 (OpenJPA) Following situation @Entity public class A{
Suppose I have the following situation: 9 class Program 10 { 11 public static

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.