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

The Archive Base Latest Questions

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

Say I have a Listener class. I have class A and class M which

  • 0

Say I have a Listener class.

I have class A and class M which both implement the listener class.
Class A has 4 M’s.

Each M has 1 A, but not as an A, as a Listener base class. (So downcasted A because M knows nothing about A).

When A gets a message from 1 of them M’s it needs to be able to know which M sent it.

Therefore, every method in the Listener class has a Listener* parameter.

so if I have something like this:

void A::someListenerMethod(Listener* l, MsgEnum msg)
{
    if(l == m_mInstance[0])
    {
       //will this work if the caller was indeed the M instance in question?
    }

}

So essentially what I’m asking is, do I need to downcast the M’s to Listeners before comparing them?

I read that sometimes C++ will make a separate object for a sub class for multiple inheritance. There is no multiple inheritance in this case, but I want to make sure this will work.

Thanks

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

    It will work as you expect.

    In fact, in order to do the comparison, m_mInstance[0] will implicitly be cast to Listener* in order for the types to match. if (l == (Listener*)(m_mInstance[0])) ... would compile to the same target code. This will typically incur no overhead unless you have multiple inheritance, in which case it might incur a minimal overhead.

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

Sidebar

Related Questions

I have a public class which has the following method and instance variable: public
Consider this scenario we have a class A which is singleton it has a
Lets say I have a listener interface defined as: class Listener { public: virtual
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have an ASMX web service, MyService. The service has a method, MyMethod.
Say I have a class named Frog, it looks like: public class Frog {
I've a text field which extends javax.swing.JTextField and also have document listener like this
let's say that i have this button which is called Click_Here and i added
I have a basic class for detecting collisions but I can't figure out how
Say I have the following Hibernate-mapped class: public class ClassA { @OneToMany(fetch=EAGER) private List<ClassB>

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.