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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:44:45+00:00 2026-06-08T19:44:45+00:00

I have to write definition of the code below. I undarstand some basics of

  • 0

I have to write definition of the code below. I undarstand some basics of OOP Cpp, I know what is *x or &x, but that’s not enough… The main fact is that i cant understand line B* p = new D, m, *k; i cant understand what m and *k stand for.

class B {
  public: 
    virtual void msg() { cout << "classB"; }
};

class D: public B {
   public: 
     virtual void msg() { cout << "classD"; }
};

int main() {
  B* p = new D, m, *k;
  p->msg(); k = &m; k->msg();
  . . .
}

Help, if you can explain how (and why so) this code will work.

thanks, for your time.

  • 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-08T19:44:48+00:00Added an answer on June 8, 2026 at 7:44 pm

    It declares multiple variables at once. This is basically the same as:

    B *p = new D;
    B  m;
    B *k;
    

    So p is a pointer to an instance of D allocated with new. m is a local instance of class B and k is a pointer to B that is later assigned to point at m.

    The msg function is called on both allocated objects via the pointers p and k.

    Note that * applies to each variable declaration separately. So B* a, b; doesn’t declare two pointers, but instead declares one pointer and one local object. This is the reason, that many people prefer to write the * directly in front of the variable name: B *a, b makes this a bit more obvious.

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

Sidebar

Related Questions

I have the pleasure to write some code that moves around stuff in an
I have write this code but it does work only if I am already
I have the below code that links and runs fine in 32bit mode -
I have a method (shown below) that I discovered can be reused in code
I have some code that does custom drawing. Basically it is form fill program
I have some code in python, that bitwise or-equals b to all the values
see i have one complex code but i m stuck up some where so
I have some code in a reusable class that modifies some types. Here's a
I'm writing a simple web app in PHP that needs to have write access
I have to write a SNMP module which monitor a certain server application 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.