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

The Archive Base Latest Questions

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

Consider the following code: struct data { int foo; int bar; }; data a;

  • 0

Consider the following code:

struct data
{
    int foo;
    int bar;
};

data a;
a.foo = 200;
a.bar = 300;

static void update(data* a, int rspec)
{
  if (!rspec) //my data management
  {
      3rdPartyApi->CreateStream();
      3rdPartyApi->PushData(a->foo);
      3rdPartyApi->PushData(a->bar);
      3rdPartyApi->CloseStream();
  }
  else // internal data management
  {
      3rdPartyApi->CreateStream();
      3rdPartyApi->PushData(3rdPartyApi->BufferQueue);
      3rdPartyApi->CloseStream();
  }
  3rdPartyApi->PushStream(3rdPartyApi->GetLastStreamBuffer().POD());
}

Lets say I change the value of a.foo or a.bar, and it requires me to call Update there-after the assignment. Can this be done, without actually calling Update() on each change manually?

[EDIT]
Note that the update function created is also assigned to a function pointer for
the third party API, so it can do it’s own internal updating. So making the update function non-global is impossible, and thus is why the current update function is global.
[EDIT]
I also rewrote my example to be more understanding and correct to the actual API I’m using

e.g

3rdPartyApi->StreamUpdate((void (*)(void*, int))update);
  • 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-23T09:30:47+00:00Added an answer on May 23, 2026 at 9:30 am

    Yes, you can. Use class methods for this. Pass a static method from your class to the 3rd party API as an update function.

    class data
    {
    public:
        void set_foo(int new_foo);
        void set_bar(int new_bar);
    
        int get_foo() const;
        int get_bar() const;
    
        // This is the update signature which the 3rd party API can accept.
        static void update(void* ptr, int rspec);
    
    private:
        // These are private so we can control their access.
        int foo;
        int bar;
    };
    
    void data::set_foo(int new_foo)
    {
        foo = new_foo;
        // 'this' is a special pointer for current data object.
        update(this);
    }
    
    void data::set_bar(int new_bar)
    {
        bar = new_bar;
        update(this);
    }
    
    int data::get_foo() const
    {
        return foo;
    }
    
    int data::get_bar() const
    {
        return bar;
    }
    
    // This is needed if the 3rd party API can only call C bindings.
    // If it's a C++ API this is not needed.
    extern "C" {
    
    void data::update(void* ptr, int rspec)
    {
        if (!rspec) //my data management
        {
            // You have to cast to data* from void*.
            data* data_ptr = reinterpret_cast<data*>(ptr);
    
            3rdPartyApi->CreateStream();
            3rdPartyApi->PushData(data_ptr->foo);
            3rdPartyApi->PushData(data_ptr->bar);
            3rdPartyApi->CloseStream();
        }
        else // internal data management
        {
            3rdPartyApi->CreateStream();
            3rdPartyApi->PushData(3rdPartyApi->BufferQueue);
            3rdPartyApi->CloseStream();
        }
        3rdPartyApi->PushStream(3rdPartyApi->GetLastStreamBuffer().POD());
    }
    
    } /* extern "C" */
    

    Then:

    3rdPartyApi->StreamUpdate(&data::update);
    data a;
    a.set_foo(200);
    a.set_bar(300);
    

    Note that use of a struct instead of a class is equally fine here. But the convention is to use classes in C++. There is only a minor difference which you can learn later.

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

Sidebar

Related Questions

consider the following code:- struct mystruct { int data; struct mystruct *next; }; void
Consider the following code: template <int dim> struct vec { vec normalize(); }; template
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code: private static void WriteProcesses(StreamWriter sw, DateTime d) { sw.WriteLine(List of
Consider the following C++ code: struct X { int a; int b; }; X
Consider the following code: #include <iostream> struct X{ X(){ throw 0; } }; void
Consider the following code: #include<iostream> #include<utility> struct Base { int baseint; }; struct Der1
Consider the following code and its output: Code #!/usr/bin/perl -w use strict; use Data::Dumper;
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>

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.