0

I am a newbie in C++ and now I am writing some codes to practice. My question may be stupid and I have tried to search for answers but what I found are not in my case (I think so, probably I am so limited to my knowledge in C++..). Okay my problem is:

I have a main function which looks like:

#include "LoadMedia.h"
#include "Parser.h"
#include "Runner.h"
#include <string>

int main(int argc, char*argv[])
{
 LoadMedia script = LoadMedia(); // initialize an object of class LoadMedia
 script.loadScript("myscript.efa");  // loadScript is a function defined in class LoadMedia
 script.Parse(); // Parse is a function defined in class Parser, class LoadMedia is inheritaged from class Parser

 Runner tempRunner = Runner(); // initialize an object in class Runner
 tempRunner.Eval(); // Eval is a function defined in class Runner

 ...... // some other codes....

}

When calling the function

Parser::Parse()

I will get some values (int, string, double) and the function

Runner::Eval() 

will need these values to do some evaluation.

I was wondering how to save the values obtained from Parse() somewhere where Eval() can also access these values easily...I am thinking whether I can use pointer or reference but I have no idea how to do it since I am so new in c++....Any ideas?

gladys0313
  • 2,569
  • 6
  • 27
  • 51
  • What is the return type of Parser::Parse()? – gcandal Nov 12 '15 at 11:00
  • I think this is too vague to really be answerable. Some ideas as to what look into though are: `Parser::Parse()` could return another class or data structure which contains the values, and then you can pass that to `Runner::Eval()`. Alternatively, it might make more sense to pass the Script object to `Runner::Eval()`? – notmyfriend Nov 12 '15 at 11:02
  • @gcandal many types, including int, double, string...would it be a good idea to use a vector or map to save the values? – gladys0313 Nov 12 '15 at 11:41
  • @gladys0313 `std::vector` and `std::map` are homogeneous, i.e. they can't store different types. If you want to return values of different types, make a struct for them or use a `std::tuple`. – TartanLlama Nov 12 '15 at 11:43
  • @notmyfriend thanks for your comments! What do you mean by "pass the script object to..." – gladys0313 Nov 12 '15 at 11:43
  • @TartanLlama thanks for your comments! – gladys0313 Nov 12 '15 at 11:44
  • @gladys0313 this kind of thing should be covered in your introductory text. If you don't have one, I'd recommend getting one from [this list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – TartanLlama Nov 12 '15 at 11:51

2 Answers2

1

If you really need to store these values you can take a look at the Parser.h to see which type is returned and store the same stype in your main.

If you don't care about storing values you can just do something like this :

tempRunner.Eval(script.Parse());

It will pass the returned values to Eval() automatically when parse has finished his job.

Nicolas Charvoz
  • 1,509
  • 15
  • 41
0

You can have 3 member variables int, string, double in LoadMedia class or Parser class (depending on design) Inside Parser :: Parse(), fill these variables. And then pass the Parser object to Runner :: Eval()

of course, this is just one way.

NightFurry
  • 358
  • 1
  • 17