4

I'm trying to create a few classes which all are derived classes from an abstract class (lets call it) BaseClass. In BaseClass I want to operate on static variables (arrays) declared in derived classes. Is there a clever way in C++ to let the compiler know that static variable will be declared in derivered class? Or should I, for instance, in my derived classes' constructors pass a reference to a static variable to base class construct? This is my idea:

class BaseClass
{
   std::vector<float> &vector;

public:
   BaseClass(std::vector<float> &dVector):vector(dVector){};

   void vectorOperation()
   {
       vector.doSomething();
   }
   ...     
}

class DerivedClass : public BaseClass
{
   static std::vector<float> sVector;
   DerivedClass():BaseClass(sVector){};
   ... 
}

Is my solution correct? Is there any better way to do this?

3
  • 2
    Sry, just noticed it was static. You could use a CRTP I suppose. Commented Dec 3, 2014 at 21:52
  • The naming is certainly not the best choice: std::vector<float> &vector; should be s.th. like std::vector<float> &vector_; at least. Commented Dec 3, 2014 at 21:53
  • Your solutions seems simple, elegant and efficient for the purpose you describe. Commented Dec 3, 2014 at 22:04

2 Answers 2

3

You could use the CRTP pattern but I am not convinced it is better than what you have.

template <typename Derived>
class BaseClass
{
   std::vector<float> &vector;

public:
   BaseClass(): vector(Derived::getVector()){};

};

class DerivedClass : public BaseClass<DerivedClass>
{
   public:

   static std::vector<float>& getVector()
   {
      static std::vector<float> v;
      return v;
   }

   DerivedClass() {};
};
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure this would work how I want it to, because v is declared in getVector() method, not in class. Would such declaration make v the same vector for every object from DerivedClass?
Yes. Since it is a static member variable, there will be only one of them.
2

In BaseClass I want to operate on static variables (arrays) declared in derived classes.

A virtual instance member function can delegate the work to an override from a derived class, which could do the access for you. There is no way of doing it in the static context (it does not look like you are trying to do it, though).

should I, for instance, in my derived classes' constructors pass a reference to a static variable to base class constructor?

This can be a solution as well. However, all instances of the derived class would be accessing the same static data region, and need to store a reference to it.

A solution based on a virtual function does not require an additional storage for a reference to the vector:

class BaseClass
{
protected:
   virtual std::vector<float>& vector() = 0;

public:
   void vectorOperation()
   {
       vector().doSomething();
   }
   ...     
};

class DerivedClass : public BaseClass
{
   static std::vector<float> sVector;
protected:
    std::vector<float>& vector() { return sVector; }
   ... 
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.