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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:22:20+00:00 2026-06-04T09:22:20+00:00

Is this the only way of overloading the ostream& operator<< for a derived class

  • 0

Is this the only way of overloading the ostream& operator<< for a derived class without duplicating code for the base class? Are the casts not something to be avoided?

I don’t see any other way, except to define some kind of function in the base class that will represent the data of the base class as something that std::operator<< could “eat up” (like a string?), doing the same thing for the derived class (calling the base class stream representation function within the derived class stream. rep. function of course).

What is the ideal solution to this problem?

#include <iostream>

class Base
{
    private: 
        int b_;
    public: 
        Base()
            :
                b_()
        {};

        Base (int b)
            : 
                b_(b)
        {};

        friend std::ostream& operator<<(std::ostream& os, const Base& b);
};

std::ostream& operator<< (std::ostream& os, const Base& b)
{
    os << b.b_; 
    return os;
}

class Derived
:
    public Base
{
    private: 
        int d_;
    public: 
        Derived()
            :
                d_()
        {};

        Derived (int b, int d)
            : 
                Base(b), 
                d_(d)
        {};

        friend std::ostream& operator<<(std::ostream& os, const Derived& b);
};

std::ostream& operator<< (std::ostream& os, const Derived& b)
{
    os << static_cast<const Base&>(b) << " " << b.d_; 
    return os;
}



using namespace std;


int main(int argc, const char *argv[])
{
    Base b(4);

    cout << b << endl;

    Derived d(4,5);

    cout << d << endl;

    return 0;
}
  • 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-04T09:22:21+00:00Added an answer on June 4, 2026 at 9:22 am

    Well … casting should be avoided if done in contexts where the result is not correctly defined, but casting into a base is always safe.

    It is possible to avoid the explicit cast by considering that a derived reference decays into the base reference, so you can use an implicit conversion, like in this case:

    std::ostream& operator<< (std::ostream& os, const Derived& b)
    {
        const Base& bs = b;
        os << bs << " " << b.d_; 
        return os;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if return *this is the only way we could return an
Is this the only way to create custom model validation? To do it using
Similar to this question only the other way of flow. Insert Picture into SQL
So I am getting pretty frustrated with this and feel the only way to
Is this: ByteBuffer buf = ByteBuffer.allocate(1000); ...the only way to initialize a ByteBuffer ?
I'm looking for a way to target this jquery only at src url's that
Is there a way to make the UIPickerView read-only? This means that the user
I wrote this Node class and = operator overload function and this is the
In languages like Java, overloading can be used in this way: void test($foo, $bar){}
This only happens in some IE's. Here: http://animactions.ca/Animactions/volet_entreprise.php You may notice that when you

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.