0

I am pretty much a relative beginner in coding doing Mech Engineering, with no advanced knowledge about pointers and stuff, basically using learncpp.com on half a screen, and the IDE on the other half, so please bear with my 'novicity'/'noviceness'.

I am using dynamic memory allocation using std::vector, and am trying to understand if I can call a function as a parameter to another function the way I will tell you below. I did research on this, and the closest answer I could find is here: Using a function as a parameter to another function, but for some reason I am unable to connect my problem with the answer to his problem, and this is the closest answer I could find between many sites I follow, so there is that. I am not even sure of what exactly is it that I am doing, so putting it in words is tough for me.

okay so if I want to do a1*((a+b)/(a-b)) "everything element wise, everything a 2D vector"

the normal way to do it would be(vecd2 is a typedef of 2D vector)

vecd2 sumew,subew,divew,ans,a,b;

loop
sumew=a+b
loop end

loop
subew=a-b
loop end

loop
divew=sumew/subew
loop end

loop
ans=a1*divew
loop end

now, say I have created functions for a+b, called sum, a-b, called sub, a*b, called mult, and a/b, called div

then is it possible to get the same answer as above using

vecd2 ans=mult(a1,div(sum(a,b),sub(a,b)))

The individual functions have the loops in them properly.

I know matlab does them, but I don't know if c++ has the same functionality as matlab here.

I am not keen on using add-on C++ libraries. I want this done in pure C++11 code.

EDIT: I am planning on learning pointers, I know they are important, but if it is possible for me to do things without them, I would rather do them so. I plan on learning and implementing pointers later, once I am more confident in the way I handle more basic C++ coding. If it is absolutely not possible without pointers, lead on!

Community
  • 1
  • 1
  • Not sure what your question is but you don't need function pointers for this. Just make functions that take vectors as arguments and return vectors. – Galik Mar 19 '17 at 13:19
  • That is what I am doing. All the functions have vectors as arguments and returns. So are these two approaches legal and equivalent? – Ayush Agrawal Mar 19 '17 at 13:23
  • I'm not familiar with Matlab but.. http://ideone.com/7njUEM I assume would be what you are trying to do? Not exactly sure what `div` will do since I am unaware that you can divide two vectors. Same for `mult`. I assumed `dot product` or multiplication by a scalar. – Brandon Mar 19 '17 at 13:29
  • not exactly. Ultimately he is not calling a function as an input to another function, he is using struct. I am trying to use the output of sub(a,b) and sum(a,b) as input for div(arg1-sum, arg2-sub), which is then used as an argument for mult(a1, arg2-div). – Ayush Agrawal Mar 19 '17 at 13:34
  • So you want function pointers as the first parameter.. http://ideone.com/ba8fAO I guess.. and for C++11: http://ideone.com/nVqdvh – Brandon Mar 19 '17 at 13:46
  • Yup, that is what I wanted. Thanks! I don't have rep to upvote the comment, so it would be great if you could write it as an answer, and I'll select it so that others can see that as the answer too. Thanks again! – Ayush Agrawal Mar 19 '17 at 14:01

0 Answers0