Is it possible to define a certain method in the parent class so that different descendants have a different number of input parameters for it? Let it be considered, for example, that these parameters can only be 3, 4 or 5, they have a type known in advance (in my case it is one int (the number of subsequent parameters, I want to get rid of it) and 2-4 parameters of the type Particle I created) ; the return value is of type float. That is, I would like, for example, to do this:

class PotentialAbstract { public: virtual float E(int particlesNumberInFormula, ...) = 0; }; // потомок, метод которого зависит от трёх параметров class PotentialForBond : public PotentialAbstract { public: float E(int particlesNumberInFormula, Particle p1, Particle p2); } // потомок, метод которого зависит от четырёх параметров class PotentialForAngle : public PotentialAbstract { public: float E(int particlesNumberInFormula, Particle p1, Particle p2, Particle p3); } // и так далее 

The particle particlesNumberInFormula, however, I, in fact, do not need, because every potential knows without it what number of particles it depends on. Simply, as I understand it, to parse the parameters in the called method, you need to get the address of the first one, only therefore I pass such a variable to the method. Next, you have to work with raw pointers; when using shared_ptr, how to act is unclear to me. Ideally, I would like the idea of ​​a fast-working method. For example, it seems to me that using std :: vector as an input parameter will be slow (or am I wrong?). Well, just out of curiosity: is it possible to make descendants with different numbers of input parameters in the same method being redefined? Thanks in advance for your reply.

  • In general, I am writing a program for calculating molecular dynamics, the goal is training in OOP. But in the issue of calculating the potential, for which I am doing these classes, I would like to get the maximum speed. It is clear that later I will have problems of another kind. But such a question arose, now in any case it is interesting to know the answer. - ⷶ ⷩ ⷮ ⷪ ⷩ
  • one
    Of course not. And how are you going to call it, having a pointer to the base class? - VladD
  • Um, really, I do not know how. Thank. Something did not even think about it. Well, I have a low level of C ++, so there are stupid questions. - ⷶ ⷩ ⷮ ⷪ ⷩ

1 answer 1

Use std :: vector, just pass it by reference or pointer to avoid unnecessary copying. I do not think that it may be a bottleneck in your program.

  • That is, it is better to do it this way: vector <Particle> v; v.reserve (2); // well or not 2, but 3 or 4? I just read that memory is allocated immediately for several elements. And of course, since I know in advance that I have no more than four elements in a vector, I would like the extra memory not to stand out. - ⷶ ⷩ ⷮ ⷪ ⷩ
  • one
    Memory is allocated as the vector is filled. Yes, you can reserve a place in advance for all items, if you know how many will be there. But again, this could get rid of unnecessary memory allocations and bring some performance gain on a large number of elements, ~ 100k. With an amount of 4, you probably won't notice anything. - Alexander
  • Excellent thank you! - ⷶ ⷩ ⷮ ⷪ ⷩ