2

I have an doubt, with C++ redefinition. I assign memory in Derived class, so I need that this memory is reserved in Base class. So, I need considered that the attibute in the Base class is the same that the attribute in the Derived class, and I don't know is that is possible in C++.

class Base {
 protected:
  float * a;
  Base() {}
 public:
  virtual void reset() {
    a = 0;
  }  
  virtual void exec() {
    printf("Memory a: %x\n",a);
  }
};  

class Derivada: virtual public Base {
 protected:
  float * a;
  Derivada() {}
  virtual void reset() {
    a = new float[256];
  }
};

int main() {
  Derivada *hija= new Derivada();    
  hija->reset();
  hija->exec();
  delete hija;
}

I really need do overloading, because it is an example of my real problem. I have the same test (Derived an main, code), for two different class Base, one in each branch, of my two branchs, of CVS.

In one of this class Base, I have this attributes, an in the other class Base, I don't have this attributes, so I have to put it, in Derived class, for compiling.

I wouldn't like to have to make two different test codes, so I need override the attribute

manlio
  • 18,345
  • 14
  • 76
  • 126
user3480234
  • 65
  • 1
  • 5
  • 1
    You only need to declare the variable in the base class. The derived class should use the base class's variable. – Jerry Jeremiah Mar 31 '14 at 08:12
  • 1
    You should really put a `std::vector` in `Base`, then `resize(256)` in the `Derived::reset()` if that's useful for you. You don't need two `float* a` in each class. Currently, you don't set `a` to `nullptr` in `Base`, nor use copy constructors, assignment or destructors to manage the lifetime of `a`: the memory is always leaked. A `vector` will be easier and safer. The `printf()` format for pointers is `%p`. – Tony Delroy Mar 31 '14 at 08:20
  • I really need do shadowing, because it is an example of my real problem. I have the same test (Derived an main, code), for two different class Base, in two different branch of cvs. In one of this class Base, I have this attributes, an in the other class Base I don`t have this attributes, so I have to put it, in Derived class, for compiling. I would like not to make two different test code – user3480234 Mar 31 '14 at 08:42

2 Answers2

3

Do not redeclare the member the the derived class. The word "protected" ensures visibility.

If you redeclare the member, you will have a new member. This is called shadowing. See, e.g.

Christian Fries
  • 16,175
  • 10
  • 56
  • 67
  • I really need do shadowing, because it is an example of my real problem. I have the same test (Derived an main, code), for two different class Base, in two different branch of cvs. In one of this class Base, I have this attributes, an in the other class Base I don`t have this attributes, so I have to put it, in Derived class, for compiling. I would like not to make two different test code – user3480234 Mar 31 '14 at 08:41
  • In that case you should maybe reformulate your question. I am not sure if inheritance is the best way to implement that test. Suggestions: let the test class have a getter for that field and your test code sould only use the getter. Then, in the getter you can have s.th. like "if base class == XXX then base.getA() ... else". – Christian Fries Mar 31 '14 at 10:57
3

You could do something like this (but requires C++11):

#include <type_traits>

// A way to check for 'attr' data member presence/absence using SFINAE
template<class T> constexpr auto missing_attr(T) -> decltype(T::attr, bool())
{
  return false;
}
constexpr bool missing_attr(...) { return true; }

struct Base { /* There might be (or not) a float data member 'attr' */ };

template<bool> struct Inject;
template<> struct Inject<true> { float attr = 0.0; };
template<> struct Inject<false> {};

struct Derived : public Base, protected Inject<missing_attr(Base())>
{
  void do_something_with_attr()
  {
    // Derived has only one 'float attr' (it can be from Base or from Inject).
    a *= a;
  }
};

There are other ways to detect if a data member exists, e.g. the member detector idiom (C++03 compatible).

manlio
  • 18,345
  • 14
  • 76
  • 126