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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:38:25+00:00 2026-05-15T16:38:25+00:00

I’m designing classes for my application (network tool). I this base class: class Descriptor

  • 0

I’m designing classes for my application (network tool). I this base class:

class Descriptor
{
    // ...

    public:
        virtual void data_read (void);
        virtual void data_write (void);
        virtual void data_error (void);

     protected:   
        int socket_descriptor;
    // ...
}

class TcpClient :
    public Descriptor
{
    // ...
}

Many classes are based on Descriptor. I monitor sockets’ events using epoll. When I want to look for events on TcpClient object I add the object’s socket and pointer to this object to epoll, code:

epoll_event descriptor_events;

descriptor_events.events = EPOLLIN;
descriptor_events.data.fd = this->socket_descriptor;
descriptor_events.data.ptr = this;

epoll_ctl (epoll_descriptor, EPOLL_CTL_ADD, this->socket_descriptor, &descriptor_events);

I dispatch epoll events in separate thread in this way:

Descriptor *descriptor (NULL);

// ...

return_value = epoll_wait (epoll_descriptor, events, 64, -1);

while (i < return_value)
{
    descriptor = (Descriptor *) (events [i]).data.ptr;

    if ((events [i]).events & EPOLLOUT)
        descriptor->data_write ();
    if ((events [i]).events & EPOLLIN)
        descriptor->data_read ();
    if ((events [i]).events & EPOLLERR)
        descriptor->data_error ();

    i++;
}

Program is going to handle a lot of data in the epoll thread, so it mean, that virtual functions will be called many times there. I’m wondering about runtime cost of this solution.

I’m also thinking about two other implementations (however I’m not sure if the’re much faster):

typedef void (*function) (Descriptor *) EventFunction;

class Descriptor
{
    // ...

    public:
        EventFunction data_read;
        EventFunction data_write;
        EventFunction data_error;

     protected:   
        Descriptor (EventFunction data_read,
                    EventFunction data_write,
                    EventFunction data_error);

        int socket_descriptor;
    // ...
}

or use CRTP.

Maybe you have other idea of implementing this?

  • 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-15T16:38:25+00:00Added an answer on May 15, 2026 at 4:38 pm

    Unless proven otherwise, your original design looks fine to me.

    The first rule of optimization is to measure first, then fix only hotspots that really exist. You’ll be surprised where your code spends its time. Dwelling on the distinction between virtual functions and function pointers is almost certainly premature optimization. In both cases the compiler will generate code to jump to a function pointer, though with virtual functions the compiler will have to look up the vtable first. Write idiomatic C++ code to do what you want, then profile it if you have performance problems.

    (I do have one comment about your class Descriptor: Unless you’re planning on having generic data_read(), data_write(), and data_error() methods I’d recommend making them pure virtual methods.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.