Advertisement
Nattack

Untitled

Feb 3rd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4. #include <thread>
  5.  
  6. #include "a.hpp"
  7. #include "b.hpp"
  8. #include "myfunctions.hpp"
  9.  
  10. int main( int argc, char** argv ) {
  11.     int n = 10;
  12.  
  13.     //stop warning me about not using args!
  14.     for (int i = 0; i < argc; i++)
  15.     {
  16.         std::cout << "argv["<< i << "of" << argc-1 <<"]: " << argv[i] << "\n";
  17.     }
  18.  
  19.     //test threads with -pthread
  20.     A* my_a = new A;
  21.     std::thread t0;
  22.     std::thread t1(myFunctions::callFromThread);
  23.     std::thread t2(myFunctions::accessFromThread, my_a);
  24.     std::thread t3(myFunctions::accessFromThread, my_a);
  25.     std::thread t4(myFunctions::callFromThread);
  26.     std::thread t5(
  27.         []() -> void
  28.         {
  29.             for (int i = 0; i < 100; i++)
  30.                 std::cout<<"l"<<i<<" ";
  31.         }
  32.     );
  33.  
  34.     //declare lambdas
  35.     auto a = [](){std::cout << "my lambda\n";}; //No capture
  36.     auto b = [&n](){std::cout << n << "\n";}; //Capture n by reference
  37.     auto c = [](const auto& var){std::cout << var << "\n";}; //one parameter by reference
  38.     auto d = [](const auto* var){std::cout << *var << "\n";}; //one parameter pointer
  39.     auto e = [=](){std::cout << n << '\n';}; //copy capture all
  40.     auto f = [n](){std::cout << n << '\n';}; //copy capture n
  41.     auto g = [&](){std::cout << n << '\n';}; //capture all by reference
  42.     auto h = [](){return 4;}; // no cap return int implicitly
  43.     auto i = [](const auto var){std::cout << var << "\n";}; // no cap one param
  44.     auto j = [&, n](){std::cout << n << "\n";}; // capture all by reference, copy capture n
  45.     auto k = []() -> char { return 42; }; // return explicit char #42
  46.     auto l = []() -> const std::string { return std::string("test string"); }; //explicit std::string return
  47.     auto m = []() -> const std::string { return "test string cstyle"; }; //explicit std::string return with a cstyle string
  48.     n = 15; //change n to something else
  49.  
  50.     // run all functions
  51.     a(); n++;
  52.     b(); n++;
  53.     c(n); n++;
  54.     d(&n); n++;
  55.     e(); n++;
  56.     f(); n++;
  57.     g(); n++;
  58.     std::cout << h() << '\n'; n++;
  59.     i(n); n++;
  60.     j(); n++;
  61.     std::cout << k() << '\n'; n++;
  62.     std::cout << l() << '\n'; n++;
  63.     std::cout << m().size() << '\n'; n++;
  64.  
  65.  
  66.     //test class A
  67.     A* object = new A(5);
  68.     A* objectTwo = new A;
  69.  
  70.     std::cout << object->getVar() << "\n";
  71.     std::cout << objectTwo->getVar() << "\n";
  72.     std::cout << objectTwo->yourF() << '\n';
  73.  
  74.     delete object;
  75.     delete objectTwo;
  76.  
  77.     //test a class that inherits from A
  78.     B* objectB = new B;
  79.  
  80.     std::cout << objectB->getVar() << "\n";
  81.     std::cout << objectB->yourF() << "\n";
  82.  
  83.     delete objectB;
  84.  
  85.     //test a class in a unique ptr
  86.     auto b_owner = std::make_unique<B>();
  87.  
  88.     std::cout << "unique pointer yourF() result: " << b_owner->yourF() << "\n";
  89.     std::cout << "unique pointer getVar() result: " << b_owner->getVar() << "\n";
  90.  
  91.     b_owner.reset();
  92.  
  93.     //test a class in a unique ptr while initializing it.
  94.     auto a_owner = std::make_unique<A>(10);
  95.  
  96.     std::cout << "unique pointer yourF() result: " << a_owner->yourF() << "\n";
  97.     std::cout << "unique pointer getVar() result: " << a_owner->getVar() << "\n";
  98.  
  99.     //test a class in a function
  100.     myFunctions::testClassInAFunction();
  101.  
  102.     //test a class from a function
  103.     auto c_owner = myFunctions::testClassReturnsFromFunction();
  104.  
  105.     //join all threads.
  106.     t1.join();
  107.     t2.join();
  108.     t3.join();
  109.     t4.join();
  110.     t5.join();
  111.  
  112.     // signifies the end of the program, anything that happens after this line is automatic
  113.     std::cout << "The End.\n";
  114.  
  115.     return 0;  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement