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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:44:37+00:00 2026-06-05T22:44:37+00:00

In C++, i have: //Base1.h #ifndef BASE1_H #define BASE1_H #include <iostream> #include <string> #include

  • 0

In C++, i have:

//Base1.h
#ifndef BASE1_H
#define BASE1_H
#include <iostream>
#include <string>
#include "Base2.h"

using namespace std;

class Base1{
    private:
        string name;
    public:
        Base1(string _name);
        void printSomething();
        string getName();
};
#endif

In Base1.cpp i implement constructor Base1(string _name) and string getName() as normal, and the printSomething():

void Base1::printSomething(){
    Base2 b2;
    // how to pass a parameter in the following code?
    b2.printBase1();
}

// This is Base2.h
#ifndef BASE2_H
#define BASE2_H

#include <iostream>
#include <string>
#include "Base1.h"

using namespace std;

class Base2{
    public:
      Base2();
      void printBase1(Base1 b);
};
#endif

And Base2() constructor i implement as usual, this is my printBase1(Base1 b):

void Base2::printBase1(Base1 b){
    cout << b.getName();
}

So, in the end, i want to use printSomething() in Base1 class, but i don’t know how to pass parameter to the b2.printBase1() in printSomething() as above in my code. is there anything like b2.printBase1(this) in C++? If not, can you give me a suggestion?

  • 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-05T22:44:38+00:00Added an answer on June 5, 2026 at 10:44 pm

    Since this is a pointer in C++, you need to dereference it:

    b2.printBase1(*this);
    

    Note that you have circular includes, you should remove #include "Base2.h" from Base1.h. Also look into passing parameters by (const) reference, especially for non-POD types, otherwise you might not get the expected behavior.

    For example, your signature is

    void printBase1(Base1 b);
    

    when you call it, you create a copy of the parameter in the function, and thus operating on a copy. You should change this to:

    void printBase1(Base1& b);
    

    or

    void printBase1(const Base1& b); //if you don't change b
    

    Pass by value only when you’re certain you need a copy.

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

Sidebar

Related Questions

This is the dependent class header #ifndef TransHeader #define TransHeader #include <string> #include CipherHeader.h
I have this abstract base class ODESolver: //ODESolver.h #ifndef ODESolver_H #define ODESolver_H #include doublependulum.h
I have a base class called Item : #ifndef ITEM_H #define ITEM_H #include <ostream>
I have something similar to Base.h #ifndef BASE_H #define BASE_H class Base { virtual
So I have a base class with the following header file: #ifndef ODESolver_H #define
Lets assume we have the following code: abstract class Base1 { protected int num;
I have a base class, defined as below (I'm also using DevExpress components): public
#include base.h #include <Poco/SharedPtr.h> #include <Poco/AutoPtr.h> #include <vector> using Poco::SharedPtr; using Poco::AutoPtr; namespace kroll
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have an interface with several events I have base class implementing the interface

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.