Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:23:10+00:00 2026-05-30T11:23:10+00:00

I have a class IDocument which serve as a interface for some classes. It

  • 0

I have a class IDocument which serve as a interface for some classes. It has some abstracts methods (virtual ... = 0).

I would like to do such all subclasses also have to implement an operator for serialization:

In addition to the overloaded stream operators documented here, any Qt classes that you might want to serialize to a QDataStream will have appropriate stream operators declared as non-member of the class:

I’m not even sure how I would make an abstract operator, but how do I define it nonmember?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:23:12+00:00Added an answer on May 30, 2026 at 11:23 am

    A non-member operator is a free function, pretty much like any other free function. For QDataStream, on operator<< would look like:

    QDataStream& operator<<(QDataStream& ds, SomeType const& obj)
    {
      // do stuff to write obj to the stream
      return ds;
    }
    

    In your case, you could implement your serialization like this (this is just one way of doing it, there are others):

    #include <QtCore>
    
    class Base {
        public:
            Base() {};
            virtual ~Base() {};
        public:
            // This must be overriden by descendants to do
            // the actual serialization I/O
            virtual void serialize(QDataStream&) const = 0;
    };
    
    class Derived: public Base {
        QString member;
        public:
            Derived(QString const& str): member(str) {};
        public:
            // Do all the necessary serialization for Derived in here
            void serialize(QDataStream& ds) const {
                ds << member;
            }
    };
    
    // This is the non-member operator<< function, valid for Base
    // and its derived types, that takes advantage of the virtual
    // serialize function.
    QDataStream& operator<<(QDataStream& ds, Base const& b)
    {
        b.serialize(ds);
        return ds;
    }
    
    int main()
    {
        Derived d("hello");
    
        QFile file("file.out");
        file.open(QIODevice::WriteOnly);
        QDataStream out(&file);
    
        out << d;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface like so: public interface IDocument : ISerializable { Boolean HasBeenUploaded
I have a master page that has two content sections like this (left some
I have installed Sphinx in order to document some Python modules and class I'm
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I would like to clarify how browser scripting (such as javascript/jquery) differs from server
Problem: I have a document class which contains a list of objects. These objects
I have some code which preloads images independent of their extension and acts in
I have the following Jquery which has 4 tabs which have 4 different searches
in my project i have a table named portfolio, which has an attribute called
Need some help with inclusion of outside variables. I have this file lang.class.php class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.