69894

Virtual overloaded operators >> and

<h3>Question</h3>

I need an interface that would require its subclasses to overload << and >>, but I'm not quite sure how since these operators aren't overloaded as member functions:

std::istream& operator>> (std::istream& in, Student& student) { in >> student.name >> student.group; for (int& i : student.marks) { in >> i; } return in; }

Maybe there's a way to make it a member function?


<h3>Answer1:</h3>

You could do something like this:

<pre class="lang-cpp prettyprint-override">class StudentInterface { public: virtual void readSelfFrom(std::istream& in) = 0; }; std::istream& operator>> (std::istream& in, StudentInteface& student) { student.readSelfFrom(in); return in; }

And then let users derive from StudentInterface, eg:

<pre class="lang-cpp prettyprint-override">class Student: public StudentInterface { public: void readSelfFrom(std::istream& in) override { in >> name >> group; for (int& i : marks) { in >> i; } } };
<h3>Answer2:</h3>

A general approach in such a case is to declare in the base class a virtual member function like

virtual std::ostream & out( std::ostream &os = std::cout ) const;

In derived classes the function will be overriden.

Then the operator << can look like

std::ostream & operator <<( std::ostream &os, const Base &obj ) { return obj.out( os ); }

A similar way can be defined the operator >> only in this case the virtual member function will not be constant..

来源:https://stackoverflow.com/questions/62141022/virtual-overloaded-operators-and

Recommend

  • How to close an html5 color picker?
  • The TypeScript compiler couldn't be found
  • Is there a way to import PST files into Outlook using C#?
  • Response.Redirect not always redirecting
  • Comparing two images visually
  • How to apply a function to pairs of rows in a data.frame, similar to diff?
  • Is it a good practice to user Fragment.setRetainInstance to not handle recreation?
  • Excel formula to match multiple dates across 2 columns, returning Yes or No
  • How to read XML file in c#?
  • Checking instance of non-class constrained type parameter for null in generic method
  • c# split string with long space or tabs
  • Jquery modal popup display only once per user session
  • socket.io example sometimes not connecting client side when using a reverse proxy
  • How to list all Call logs from Log list including Sms and email logs also?
  • Export a simple Dictionary into Excel file in python
  • How to make infinite threaded comments
  • Facebook friend list in Facebook Android SDK 3.14
  • Synchronous Calls with jqGrid?
  • git clone, upload-pack out of memory
  • How to load dynamic images in custom ListView
  • Conflicting declaration using constexpr and auto in C++11
  • Support of :after in IE7
  • SpringBoot: Bypass OncePerRequestFilter filters
  • reshape/remould data frame to create normalized bar chart and pie chart
  • Will this work on all screen sizes?
  • Google App Engine Datastore: Dealing with eventual consistency
  • Spring Boot fails to start
  • php “page caching” solution suggestions for CMS Applications
  • Codeigniniter insert data through models and controller
  • VLOOKUP in IMPORTRANGE
  • Angular 4: Responsive Grid List
  • Sign a Pdf using custom digital signature in Java
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Accessing Arguments, Workflow Variables from custom activities
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Spring Boot not autowiring @Repository
  • ReferenceError: TextEncoder is not defined
  • JavaScript RegExp Replace
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}