What detectable differences exist between a class and its base class?

Given the following pattern:

template <typename T>
class wrapper : public T {};

What are the visible differences in interface or behavior between a type Fooobject and a type object wrapper<Foo>?

I already know about this:

  • wrapper<Foo>only has a null constructor, a copy constructor, and an assignment operator (and it only has those if these operations are valid on Foo). This difference can be reduced by having a set of boilerplate constructors in wrapper<T>that pass values ​​through constructor T.

But I'm not sure what other detectable differences might be or if there are ways to hide them.


(Change) Concrete example

Some people seem to be asking some context for this question, so here's a (somewhat simplified) explanation of my situation.

, , . ( ) . , . - :

class ComplexDataProcessor {
    hotvar<int> epochs;
    hotvar<double> learning_rate;
public:
    ComplexDataProcessor():
        epochs("Epochs", 50),
        learning_rate("LearningRate", 0.01)
        {}

    void process_some_data(const Data& data) {
        int n = *epochs;
        double alpha = *learning_rate;
        for (int i = 0; i < n; ++i) {
            // learn some things from the data, with learning rate alpha
        }
    }
};

void two_learners(const DataSource& source) {
    hotobject<ComplexDataProcessor> a("FastLearner");
    hotobject<ComplexDataProcessor> b("SlowLearner");
    while (source.has_data()) {
        a.process_some_data(source.row());
        b.process_some_data(source.row());
        source.next_row();
    }
}

:

FastLearner.Epochs
FastLearner.LearningRate
SlowLearner.Epochs
SlowLearner.LearningRate

( , - ), . . , : hotobject<T>. hotobject<T> - ​​ / , T ( hotvar<T> , , ), .

:

struct hotobject_stack_helper {
    hotobject_stack_helper(const char* name) {
        // push onto the thread-local context stack
    }
};

template <typename T>
struct hotobject : private hotobject_stack_helper, public T {
    hotobject(const char* name):
        hotobject_stack_helper(name) {
        // pop from the context stack
    }
};

, :

  • hotobject_stack_helper ( )
  • T - T (hotvars)
  • hotobject<T>, .

, . : , . , : hotobject - ?

+3
4

( ) . , , , . , .

, . () . "is-a" "has-a" .

, , . , private ( public class), , , .

0

, ( " , " ), , , :

wrapper<T> T, :

  • a T. ( .)
  • T.
  • T .

, , .

+3

, :

class Base {};
class Derived : Base {};

:

Base *basePtr = new Derived;

:

wrapper<Base> *basePtr = new wrapper<Derived>();

, , , , .

+2

- ( ),

sizeof(InheritedClass) > sizeof(BaseClass)
0

Source: https://habr.com/ru/post/1761449/


All Articles