How do I execute a command and get the output of the command within C++ using POSIX?

I am looking for a way to get the output of a command when it is run from within a C++ program. I have looked at using the system() function, but that will just execute a command. Here’s an example of what I’m looking for: std::string result = system(“./some_command”); I need to run an arbitrary … Read more

How do I make the method return type generic?

Consider this example (typical in OOP books): I have an Animal class, where each Animal can have many friends. And subclasses like Dog, Duck, Mouse etc which add specific behavior like bark(), quack() etc. Here’s the Animal class: public class Animal { private Map<String,Animal> friends = new HashMap<>(); public void addFriend(String name, Animal animal){ friends.put(name,animal); … Read more

Best way to return multiple values from a function? [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago. Improve this question The canonical way to return multiple values in languages that support it is often tupling. Option: … Read more

How do I make the method return type generic?

You could define callFriend this way: public <T extends Animal> T callFriend(String name, Class<T> type) { return type.cast(friends.get(name)); } Then call it as such: jerry.callFriend(“spike”, Dog.class).bark(); jerry.callFriend(“quacker”, Duck.class).quack(); This code has the benefit of not generating any compiler warnings. Of course this is really just an updated version of casting from the pre-generic days and … Read more