0

This is kind of an odd question so I will do the best I can to explain myself.

Say I have the following classes:

Class1 A;
Class2 B;
Class3 B;

And each one of these classes has a setVariable and getVariable function that will set a private variable called, variable.

Now let me stop you here! You're probably asking, "Why don't you just make 1 class called Class and make a Class A, Class B, and Class C?" And this is because the current project I am working on has different classes built within that do not all have the same structure. For example lets say Class1 is an OpenGL inherited class, Class2 is a widget that has some slider with some values on it, and Class3 is a textBox with only one value.

I am just breaking it down like this to make it easier for myself to visualize. Now that we have that out of the way, let me explain what I am trying to do:

#include "class1.hpp"
#include "class2.hpp"
#include "class3.hpp"
#include "baseClass.hpp"

int main()
{
  Class1 A;
  Class2 B;
  Class3 C;

  BaseClass<Class1,Class2> base1(A,B);

  base1.setValue(30);
}

And then behind the scenes, the variable of class A and class B will be set to 30. However, this is the problem I am dealing with:

#ifndef BASE_CLASS_HPP
#define BASE_CLASS_HPP

#include <utility>

template<class A, class B>
class BaseClass
{
  public:
    BaseClass(const A &classA, const B &classB) : class1(classA), class2(classB){}
    void setValue(int);
    std::pair<int,int> getValue();

  private:
    A class1;
    B class2;
};
#endif

The problem is BaseClass has no idea about Class1, Class2, or Class3's functions. And that is because they are not #included in this file. So now your next question will be, "Well, why don't you #include every single class into there?" And that is because I do not want to have to define a BaseClass constructor for every single class that I have to create. What I do know, is that each one of these classes will have a getValue and setValue function. And that is the only thing that I want to update. Nothing more. Is there any possible way to get access to those class functions?

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52

1 Answers1

0

If you're gonna do this, you have to do a few things.

The good news is that writing it is pretty simple:

template<class A, class B>
class BaseClass {
    A& a; // store them by reference
    B& b; 
   public:
    // References have to be initialized in the member initializer list
    BaseClass(A& a, B& b) 
      : a(a), b(b) 
    {}
    void setValue(int value) {
        a.setValue(value); 
        b.setValue(value); 
    }
    std::pair<int, int> getValue() {
        return {a.getValue(), b.getValue() };
    }
};

Then, in the main function, you have to provide the classes as template arguments:

int main()
{
    Class1 A;
    Class2 B;

    BaseClass<Class1, Class2> base1(A,B);

    // This works now
    base1.setValue(30);

    std::cout << A.getValue() << '\n'; // Prints 30
}

If you do things this way, you have put BaseClass in it's own file without including any other files.

Alecto Irene Perez
  • 10,321
  • 23
  • 46