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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:24:05+00:00 2026-05-25T02:24:05+00:00

I have redefined the << operator and I want it to take a reference

  • 0

I have redefined the << operator and I want it to take a reference of pointer.

class foo
{
    foo();
    virtual ~foo();

    void operator << (BaseService*& iRight);
}

Somewhere in the code, having a foo instance, and a service which is a specialization of the BaseService class I do :

Service* service_pointer = new Service();
foo_instance << service_pointer;

But I get this error :
error: no match for ‘operator<<‘in ‘foo_instance << service_pointer’
note: candidates are: void foo::operator<<(BaseService*&)

Nothing changes if I dynamic_cast my service_pointer to BaseService

Service* service_pointer = new Service();
foo_instance << dynamic_cast<BaseService*>(service_pointer);

Any idea ?

  • 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-05-25T02:24:06+00:00Added an answer on May 25, 2026 at 2:24 am

    The first version does not work because you can’t pass in a reference to a pointer to a subtype and rightly so: What if the implementation of operator<< made the pointer point to an instance MyService (which is a subclass of BaseService, but not of Service)? Clearly it would be illegal for a Service* to point to a MyService. So passing in a Service* is not allowed.

    The second version is not allowed because dynamic_cast does not return an l-value, so you can’t pass it as a non-const reference.

    The only thing you can do is define another variable of type BaseService* and pass that as an argument to <<. If << then reassigns the pointer, that change will be visible for the newly created variable only and not affect ServicePointer.

    That being said (and not knowing your use case) I have to advice you that having operator<< take a non-const reference to anything as its right operand strikes me as bad practice. You wouldn’t usually expect << to modify it’s right operand.

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

Sidebar

Related Questions

I have some trouble with class template inheritance and operators (operator +), please have
I have defined a class in C++ which holds an array of scalars of
I have a struct defined as the following: typedef struct { string foo; }
I have an abstract class defined as follows: abstract class Abstract Parent extends Zend_Db_Table_Abstract
I want to make '==' operator use approximate comparison in my program: float values
I've got a generic range class and I'm trying to add a comparison operator
I want to add a operator override to perform assignments /__set__s inline. Template :-
I have a template base class like this: template<typename T, std::size_t Size> class VectorT
If I have class A that defines __cmp__ , and class B that extends
I have a template matrix class class defined in a header called Matrix.h. Certain

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.