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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:16:27+00:00 2026-05-26T09:16:27+00:00

Consider the following example of polymorphism in C++. To me, this is unexpected behavior,

  • 0

Consider the following example of polymorphism in C++. To me, this is unexpected behavior, which probably lies in the fact that I am still thinking too much in Java. The question to me is now: How do I get the pointer example to call the more specific method.

#include <iostream>
#include <string.h>
#include <boost/tr1/memory.hpp>

class Image {
 public:
  Image(std::string className = "Image") 
      : className_(className)
    {}

  virtual ~Image() {}

  virtual std::string className() {
    return className_;
  }

 private:
  std::string className_;
};

class RightImage : public Image {
 public:
  RightImage()
      : Image("RightImage")
    {}
};

class Processor{
 public:
  void process(Image& image){
    std::cout << "Invoking process(Image& image) with image of type \"" << image.className() << "\"" << std::endl;
  }
  void process(RightImage& rightImage){
    std::cout << "Invoking process(RightImage& rightImage) with rightImage of type \"" << rightImage.className()  << "\"" << std::endl;
  }

  void process(Image* image){
    std::cout << "Invoking process(Image* image) with image of type \"" << image->className() << "\"" << std::endl;
  }
  void process(RightImage* rightImage){
    std::cout << "Invoking process(RightImage* rightImage) with rightImage of type \"" << rightImage->className()  << "\"" << std::endl;
  }
};

int main(int argc, char **argv) {
      std::tr1::shared_ptr<Image> rightImageSharedPtr(new RightImage());
      Image* rightImagePointer = new RightImage();
      RightImage rightImage;
      Processor processor;
      std::cout << "value:                   ";
      processor.process(rightImage);
      std::cout << "shared_ptr:              ";
      processor.process(*rightImageSharedPtr);
      std::cout << "old fashioned pointer 1: ";
      processor.process(*rightImagePointer);
      std::cout << "old fashioned pointer 2: ";
      processor.process(rightImagePointer);
}

The output of that program is:

value: Invoking process(RightImage& rightImage) with rightImage of type "RightImage"

shared_ptr: Invoking process(Image& image) with image of type "RightImage"

old fashioned pointer 1: Invoking process(Image& image) with image of type "RightImage"

old fashioned pointer 2: Invoking process(Image* image) with image of type "RightImage"

How can I make the last three examples also call process(RightImage&) and process(RightImage*)?

  • 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-26T09:16:28+00:00Added an answer on May 26, 2026 at 9:16 am

    Next to the double dispatch as proposed by tokage, you could also have only 1 Process() function with the base class reference as parameter and then use polymorphism by calling virtual function of the base class inside the Process() function.

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

Sidebar

Related Questions

Please consider following example: The source image consists of 6 areas that need to
Consider the following example F# code: type mytype() = member this.v = new OtherClass()
I'm having a hard time understanding this. Consider the following example: protected void Page_Load(object
Consider the following example which should color every second row: ( live demo here
Consider the following example messages in my database: I dont agree with you That
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following example. It consists of two header files, declaring two different namespaces:
Consider the following example program: next :: Int -> Int next i | 0
Consider the following example. I have an interface MyInterface, and then two abstract classes
Consider the following example: int size = 10, *kk = new int[size]; for (int

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.