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

  • Home
  • SEARCH
  • 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 8570291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:31:30+00:00 2026-06-11T18:31:30+00:00

quick question on declaring functions. Lets say I have the following code: class a{

  • 0

quick question on declaring functions. Lets say I have the following code:

class a{
    int var;
    /* stuff*/
}

class b : a{
    /* other stuff*/
}

class c : a{
    /* more other stuff*/
}

class d{
    myFunctionThatWantsTheIntNamedVar(SubClassOfClassA SomeClassUnknownAtCompileTime);
}

and myFunctionThatWantsTheIntNamedVar() wants just that, the integer(called var) declared in the base class of a. The function in class d may be passed an instance of class b or class c, which is not known at compile time (only at run time).

Is there a way I can concisely declare a function that could take either class b or c and get that same base variable?

What I have done in the mean time is declare two methods in class d, as follows:

class d{
    myFunctionThatWantsTheIntNamedVar(c aClassCInstance);
    myFunctionThatWantsTheIntNamedVar(b aClassBInstance);
}

which is fine and all, but the code in the two methods is IDENTICAL… And from my understanding of object oriented programming, if the code is identical you should be able to combine it into a better function.

Any thoughts on this? Just looking for best practices or other advice here, as I work at a small company and getting feedback can be hard at times.

EDIT: sorry, I had an error in my code, changed it from:

myfunctionThatWantsTheInNamedVar(a aClassAInstance);

to:

myfunctionThatWantsTheInNamedVar(c aClassCInstance);

my bad.

  • 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-11T18:31:31+00:00Added an answer on June 11, 2026 at 6:31 pm

    Provide a public accessor method that gives access to the variable, and make sure you don’t hide its name in class b and class c.

    class a{
        int var;
        /* stuff*/
     public:
      const int& getVar() const { return var; }
      int& getVar() { return var; }
    };
    

    Then take a reference to an a in oyur function:

    class d{
     public:  // you probably want a public member function
        myFunctionThatWantsTheIntNamedVar(const a& theA) {
          int n = theA.getVar();
    }
    

    Then you can call it with b or c instances:

    b b0;
    c c0;
    d d0;
    d0.myFunctionThatWantsTheIntNamedVar(b0);
    d0.myFunctionThatWantsTheIntNamedVar(c0);
    

    Bear in mind that in your code, the variable var is private, as well as the inhericance in classes b and c. For this to work, you would need to make the inheritance public:

    class b : public a { .... };
    class c : public a { .... };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick Question Lets say I have $colname = 'offer_id'; and then I want to
Quick question, I have the following string which is a coma separated list of
quick question on GWT from a newbie. Considering the following code: FlowPanel block =
Quick question that requires a long explanation.. Say I have two tables - one
Quick Question: I have the following array (multidimensional): Array ( [preorder] => 0 [data]
Quick question: If I have a very large function/sub in a class that is
Quick question as likely I will have more later. I'm currently investigating document management
Quick question about the built in python list object. Say you have a list
Quick question regarding settimeout, does it execute periodically like setinterval? Say I have a
Quick question about data joining. Say I have some elements that each contain a

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.