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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:35:51+00:00 2026-06-09T22:35:51+00:00

The following is a rough sample of what the code looks like, the question

  • 0

The following is a rough sample of what the code looks like, the question is how can I have DerivedOne and DerivedTwo have a overloaded << operator but store these objects in a vector of Base*.

As for what I want to achieve; I want to be able to loop through the objects vector and output the information that I tell it to in the DerivedOne and DerivedTwo.

vector<Base*> objects;

class Base
{
 private:
 object Data
 public:
 object getData() { return Data; }
};

class DerivedOne : public Base
{
}

class DerivedTwo : public Base
{
}

Now I know there is this, but it wont work for my purposes.

friend ostream &operator<<(ostream &stream, Object object)
{
    return stream << "Test" << endl;
}
  • 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-09T22:35:52+00:00Added an answer on June 9, 2026 at 10:35 pm

    Make your virtual methods private to separate how an object is used from how its behavior can be customized by derived classes.

    Here’s a similar to other answers solution but the virtual method is private:

    #include <iostream>
    
    namespace {
      class Base {
        // private (there is no need to call it in subclasses)
        virtual std::ostream& doprint(std::ostream&) const = 0;
      public:
        friend std::ostream& operator << (std::ostream& os, const Base& b) {
          return b.doprint(os); // polymorphic print via reference
        }
    
        virtual ~Base() {} // allow polymorphic delete
      };
    
    
      class DerivedOne : public Base {
        std::ostream& doprint(std::ostream& os) const {
          return os << "One";
        }
      public:
        DerivedOne() { std::cerr << "hi " << *this << "\n"; } // use << inside class
        ~DerivedOne() { std::cerr << "bye " << *this << "\n"; }
      };
    }
    

    Example

    #include <memory>
    #include <vector>
    
    int main () {
      using namespace std;
      // wrap Base* with shared_ptr<> to put it in a vector
      vector<shared_ptr<Base>> v{ make_shared<DerivedOne>() };
      for (auto i: v) cout << *i << " ";
      cout << endl;
    }
    

    Output

    hi One
    One 
    bye One
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a piece of code with the following rough signature: void evaluate(object *
Pretty simple question but I'm having a rough morning it seems. I have the
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following chunk html code works as expected: <iframe src=http://www.amazon.com/></iframe> But when trying embed inner
I'm not sure if the following is possible but I'd like to Invoke a
I have the following Clojure code to calculate a number with a certain factorable
The following code claims that Jack is employed in construction, but Jane is yet
I am trying to do something like the following in python, but i am
I have on one of my views the following razor code: @if (item.PMApproved !=
I have to delete a shape in canvas. The following code was written- function

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.