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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:53:21+00:00 2026-06-15T16:53:21+00:00

EDIT: None of the code is mine, it was all given to me and

  • 0

EDIT: None of the code is mine, it was all given to me and I’m just analyzing it from an object allocation methods point of view.

I’m having a really hard time with memory allocation. I promise you I’ve looked at a hundred examples on here over the last 5 hours. I understand this is probably poorly worded and I apologize in advance.

1.) I don’t understand why Version 2 prints class A’s f() instead of class B’s. The line

A* objA1 = objB;

is declaring a pointer, objA1, that points to an object of type A and it is assigning its address to where objB is pointing, B(2,3). I’ve read the sentence” if they’re heap-dynamic objects, then you can assign b1 to a1″. When I call objA1->f(), am I simply saying go to this location, oh you found a B? well cast it into an A and call f().

2.) I would have thought Version 1 sliced objB, because it is saying set this object which has been allocated room for A, equal to B which is larger. But if I put cout << objB.bf; after this assignment, it is still valid. is A objA1 = objB; not statically declaring objA1? And once again, why wouldn’t this print B’s f()?

3.) What is the difference between the two objA1 assignments? As in what functionality would only work with one of the two. Could you call both “heap-dynamic” if you wanted to give it an outdated classification of sorts?

class A {
  private:
    int a;
  public:
    A(int ia) {a = ia;}
    void f() { 
      cout << "Call to method f defined in class A" << endl;
    }
};
 class B : public A {
  private:
    int b;
  public:
    B(int ia, int ib) : A(ia) {b = ib;}
    void f() { 
      cout << "Call to method f specialized in class B" << endl; 
    }
    void bf() { 
      cout << "Call to class B own method bf" << endl; 
    }
}; 
// C++ driver - Version 1
void main() { 
  A objA = A(1);
  B objB = B(2,3);

  objA.f();
  objB.f();
  objB.bf(); 

  A objA1 = objB;
  objA1.f();
}

 // C++ driver - Version 2
void main() {
  A* objA = new A(1);
  B* objB = new B(2,3);

  objA->f();
  objB->f();
  objB->bf(); 
  A* objA1 = objB;
  objA1->f();
} 
  • 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-15T16:53:22+00:00Added an answer on June 15, 2026 at 4:53 pm

    C++ does not use dynamic dispatch by default. This means that calling p->f() when p is declared as X *p will always call X::f(), regardless of the dynamic type of the instance pointed to by p. To enable dynamic dispatch, the function must be declared virtual. This is in contrast against Java/C#, where all member functions are implicitly virtual.

    The slicing in “Version 1” indeed occurs, however, objB is in no way affected by being assigned somewhere. It’s objA that is affected by slicing (i.e. it ignores the part of objB which is not part of A).

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

Sidebar

Related Questions

I have following code in my MVC2 view: <tr class=edit style=display:none> <td> <%= Html.DropDownList(drpFields,
I have following code in my MVC2 view: <tr class=edit style=display:none> <td> <%= Html.DropDownList(drpFields,
EDIT: stupid logic of mine got ahead of me. The none are just the
I'm just wondering how to improve this code : pagination.children('li.active').prev().prev().prev().prev().prevAll('li.pgn').css(display,none); I just don't like
Edit: I have looked at the answers code: NONE of them do what I
EDIT: See my answer below--> I am wanting to have a view that when
Edit: Here is the new code: $(#normalcontent).hide(fast).load($this.attr(href) + #normalcontent,,function(){ $(this).slideDown(normal); $(this).find(img).each(function(){ if($(this).hasClass(large)) { var
In my edit action of my employees_controller I have this line of code: #Employee#edit
I'm having problems converting HTML into regular expressions. I just can't seem to figure
there were a few question similar to mine on the stack but none that

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.