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

  • Home
  • SEARCH
  • 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 8884839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:09:39+00:00 2026-06-14T21:09:39+00:00

The following situation: class Main{ void MainMethod(){ C * c; B * b =

  • 0

The following situation:

class Main{
   void MainMethod(){
      C * c;
      B * b = new B();
      b->fillC(c);
   }
};

class B{
   void fillC(C* c){
      c = new C();
   }
};

class C{
};

In my software I have this situation. At the end of the program c is still empty for class Main. Why is this?

  • 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-14T21:09:40+00:00Added an answer on June 14, 2026 at 9:09 pm

    The pointer c is copied into the fillC function – this is known as passing by value. You then assign to that copy. The original c variable from MainMethod is left unaffected. You need to pass the pointer by reference if you want to change it:

    class B{
      void fillC(C*& c){
        c = new C();
      }
    };
    

    Now the c inside fillC refers to the same pointer as the c in MainMethod. Changing the value of one affects the value of the other.

    You could alternatively change the parameter of fillC to type C**, call it with fillC(&c) and assign to it with *c = new C() – but you’re already using a mishmash of idioms from other languages, so I wouldn’t recommend this.

    Don’t forget to do delete c at some point!

    • 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 am new to Hibernate and have the following situation: class A class Course
I have this situation where I declare inside my main class a function that
I have found following question: $0 (Program Name) in Java? Discover main class? but
Suppose I have the following situation: 9 class Program 10 { 11 public static
I have the following situation : abstract class X { abstract X someMethod (...)
I have following situation - Have a MongoService class, which reads host, port, database
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have the following manager<->worker situation: class Manager { private: pthread_attr_t workerSettings; pthread_t worker;

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.