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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:25:45+00:00 2026-05-28T06:25:45+00:00

My first post here :) I am having a problem with the following C++

  • 0

My first post here 🙂

I am having a problem with the following C++ code. I have an ABC class A, and two derived classes B and C. All of them have a static member called id:

using std::cout;

class A
{
private:
    friend int bar(A& a);
    static const int id = 1;

    virtual void foo() = 0;
};

class B : public A
{
private :
    friend int bar(A& a);
    static const int id = 2;

    void foo() { /*Do something*/ }
};

class C : public A
{
private:
    friend int bar(A& a);
    static const int id = 3;

    void foo() { /*Do something*/ }
};

int bar(A& a)
{
    return a.id;
}

int main()
{
    B b;
    C c;

    cout << bar(b) << "\n";
    cout << bar(c) << "\n";

    return 0;
}

I was expecting this code to print out 2 and 3 – rather it prints out 1 and 1 (bar() is always using A::id). What am I doing wrong? Any ideas?


Based on the comments below, this the final code I am using. It works, but would love to hear more thoughts 🙂

#include <iostream>

using std::cout;

class A
{
private:
    virtual void foo() = 0;
};

class B : public A
{
private:
    template <typename T>
    friend int bar(T& t);

    static const int id = 2;
    void foo() { /*do something*/ }
};

class C : public A
{
private:
    template <typename T>
    friend int bar(T& t);

    static const int id = 3;
    void foo() { /*do something*/ }
};


template <typename T>
int bar(T& t)
{
    return t.id;
}


int main()
{
    B b;
    C c;

    cout << bar(b) << "\n";
    cout << bar(c) << "\n";

    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-05-28T06:25:46+00:00Added an answer on May 28, 2026 at 6:25 am

    Is there any way to avoid writing int foo() { return id; } for all the derived classes?

    Yes, using templates. For example:

    template <typename T>
    int foo (T& x)
    {
        return x.id;
    }
    

    However, if id is private, this doesn’t reduce the code by all that much.

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

Sidebar

Related Questions

This is my first post here. I have a problem. I need to take
First post here. Having a problem with NSNumber's floatValue method -- somehow, it returns
first post here, and probably an easy one. I've got the code from Processing's
Well, this is my first post here and really enjoying the site. I have
I'm having problem with rendering depth in OpenGL Following code is a simple example
I have seen this post here (http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state). I am having a similar problem I
First post here... I normally develop using PHP and Symfony with Propel and ActionScript
first post here, I come in peace :) I've searched but can't quite find
My first post here, so i hope this is the right area. I am
This is my first post here and I wanted to get some input from

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.