Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <memory>
- #include <thread>
- #include "a.hpp"
- #include "b.hpp"
- #include "myfunctions.hpp"
- int main( int argc, char** argv ) {
- int n = 10;
- //stop warning me about not using args!
- for (int i = 0; i < argc; i++)
- {
- std::cout << "argv["<< i << "of" << argc-1 <<"]: " << argv[i] << "\n";
- }
- //test threads with -pthread
- A* my_a = new A;
- std::thread t0;
- std::thread t1(myFunctions::callFromThread);
- std::thread t2(myFunctions::accessFromThread, my_a);
- std::thread t3(myFunctions::accessFromThread, my_a);
- std::thread t4(myFunctions::callFromThread);
- std::thread t5(
- []() -> void
- {
- for (int i = 0; i < 100; i++)
- std::cout<<"l"<<i<<" ";
- }
- );
- //declare lambdas
- auto a = [](){std::cout << "my lambda\n";}; //No capture
- auto b = [&n](){std::cout << n << "\n";}; //Capture n by reference
- auto c = [](const auto& var){std::cout << var << "\n";}; //one parameter by reference
- auto d = [](const auto* var){std::cout << *var << "\n";}; //one parameter pointer
- auto e = [=](){std::cout << n << '\n';}; //copy capture all
- auto f = [n](){std::cout << n << '\n';}; //copy capture n
- auto g = [&](){std::cout << n << '\n';}; //capture all by reference
- auto h = [](){return 4;}; // no cap return int implicitly
- auto i = [](const auto var){std::cout << var << "\n";}; // no cap one param
- auto j = [&, n](){std::cout << n << "\n";}; // capture all by reference, copy capture n
- auto k = []() -> char { return 42; }; // return explicit char #42
- auto l = []() -> const std::string { return std::string("test string"); }; //explicit std::string return
- auto m = []() -> const std::string { return "test string cstyle"; }; //explicit std::string return with a cstyle string
- n = 15; //change n to something else
- // run all functions
- a(); n++;
- b(); n++;
- c(n); n++;
- d(&n); n++;
- e(); n++;
- f(); n++;
- g(); n++;
- std::cout << h() << '\n'; n++;
- i(n); n++;
- j(); n++;
- std::cout << k() << '\n'; n++;
- std::cout << l() << '\n'; n++;
- std::cout << m().size() << '\n'; n++;
- //test class A
- A* object = new A(5);
- A* objectTwo = new A;
- std::cout << object->getVar() << "\n";
- std::cout << objectTwo->getVar() << "\n";
- std::cout << objectTwo->yourF() << '\n';
- delete object;
- delete objectTwo;
- //test a class that inherits from A
- B* objectB = new B;
- std::cout << objectB->getVar() << "\n";
- std::cout << objectB->yourF() << "\n";
- delete objectB;
- //test a class in a unique ptr
- auto b_owner = std::make_unique<B>();
- std::cout << "unique pointer yourF() result: " << b_owner->yourF() << "\n";
- std::cout << "unique pointer getVar() result: " << b_owner->getVar() << "\n";
- b_owner.reset();
- //test a class in a unique ptr while initializing it.
- auto a_owner = std::make_unique<A>(10);
- std::cout << "unique pointer yourF() result: " << a_owner->yourF() << "\n";
- std::cout << "unique pointer getVar() result: " << a_owner->getVar() << "\n";
- //test a class in a function
- myFunctions::testClassInAFunction();
- //test a class from a function
- auto c_owner = myFunctions::testClassReturnsFromFunction();
- //join all threads.
- t1.join();
- t2.join();
- t3.join();
- t4.join();
- t5.join();
- // signifies the end of the program, anything that happens after this line is automatic
- std::cout << "The End.\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement