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 8231819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:37:21+00:00 2026-06-07T17:37:21+00:00

Suppose I have two classes: class1 { int m_i; std::string m_s; }; class2 {

  • 0

Suppose I have two classes:

class1 {
 int m_i;
 std::string m_s;
};

class2 {
 int m_i2;
 class1 *m_ptr;
};

Now, I want to send a class2 variable over network, and want to use any of the libraries that does serialization.(Protocol-buffers, Thrift, MessagePack..)

Which one can I use?(note the class1* m_ptr)

  • 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-06-07T17:37:22+00:00Added an answer on June 7, 2026 at 5:37 pm

    Using google protocol buffers, you would need a .proto file (say test.proto) like:

    package serialisation;  // puts this in namespace serialisation
    
    message class1 {
      required int32 m_i = 1;
      required bytes m_s = 2;
    }
    
    message class2 {
      required int32 m_i2 = 1;
      optional class1 m_ptr = 2;
    }
    

    Using C++, once you run the protoc compiler against this, you end up with test.pb.cc and test.pb.h

    You can then use these like:

    #include <string>
    #include "test.pb.h"
    
    struct class1 {
      int m_i;
      std::string m_s;
    };
    
    struct class2 {
      int m_i2;
      class1 *m_ptr;
    };
    
    int main() {
      class2 second_class;
      second_class.m_i2 = 2;
      second_class.m_ptr = new class1;
      second_class.m_ptr->m_i = 1;
      second_class.m_ptr->m_s = "one";
    
      // Serialise class 2
      serialisation::class2 serialisable_second_class;
      serialisable_second_class.set_m_i2(second_class.m_i2);
      if (second_class.m_ptr) {
        serialisation::class1* serialisable_first_class = serialisable_second_class.mutable_m_ptr();
        serialisable_first_class->set_m_i(second_class.m_ptr->m_i);
        serialisable_first_class->set_m_s(second_class.m_ptr->m_s);
      }
      std::string serialised(serialisable_second_class.SerializeAsString());
    
      // Parse class 2
      serialisation::class2 parsed_second_class;
      parsed_second_class.ParseFromString(serialised);
      class2 retrieved_second_class;
      retrieved_second_class.m_i2 = parsed_second_class.m_i2();
      if (parsed_second_class.has_m_ptr()) {
        retrieved_second_class.m_ptr = new class1;
        retrieved_second_class.m_ptr->m_i = parsed_second_class.m_ptr().m_i();
        retrieved_second_class.m_ptr->m_s = parsed_second_class.m_ptr().m_s();
      } else {
        retrieved_second_class.m_ptr = nullptr;
      }
    
      return 0;
    }
    

    Note, for the sake of brevity I’m not doing any error checking or exception handling here – this would be needed in production code. I’m also not managing the lifetime of the class1 pointer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have two classes: class Base { private: int x; public: void f();
Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose I have two classes. In the first one I declare this in Class1.h
Suppose I have two classes: public class Student { public int Id {get; set;}
My questions is, suppose we have two classes A and B. I want to
Suppose I have these two classes class base_size { public: int size() { return
I have two classes: JDialog and JFrame (see below). Now I want them to
suppose you have two (or more) classes with private member vectors: class A {
Supposed I have two classes like this: public class Parent { public string Id

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.