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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:42:26+00:00 2026-06-11T18:42:26+00:00

given: #include <stdio.h> class A { friend class B; private: void func(); } GlobalA;

  • 0

given:

#include <stdio.h>

class A
{
  friend class B;
 private:
  void func();
} GlobalA;

void A::func()
{
  printf("A::func()");
}

class B
{
 public:
  void func();
};

void B::func()
{
  GlobalA.func();
}

int main()
{
  B b;
  b.func();
  getchar();
}

so really all B::func() does is call A::func(), is there a better way to do this? Or does the compiler just call A::func() directly when it compiles.

CONSTRAINTS:
class A creates threads and is used by multiple other classes. it is a global IO class to manage sockets/pipes, so i dont believe any type of inheritance would go over well.

NOTE: If this is a google-able problem please let me know as i did not what to search.

  • 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-11T18:42:27+00:00Added an answer on June 11, 2026 at 6:42 pm

    Check out the generated assembly from your compiler (I used GCC 4.7 -O3):

    For A::func()

    _ZN1A4funcEv:
    .LFB31:
        .cfi_startproc
        subl    $28, %esp
        .cfi_def_cfa_offset 32
        movl    $.LC0, 4(%esp)
        movl    $1, (%esp)
        call    __printf_chk
        addl    $28, %esp
        .cfi_def_cfa_offset 4
        ret
        .cfi_endproc
    

    And B::func():

    _ZN1B4funcEv:
    .LFB32:
        .cfi_startproc
        subl    $28, %esp
        .cfi_def_cfa_offset 32
        movl    $.LC0, 4(%esp)
        movl    $1, (%esp)
        call    __printf_chk
        addl    $28, %esp
        .cfi_def_cfa_offset 4
        ret
        .cfi_endproc
    

    They’re identical – the compiler has done the smart thing for you for free behind the scenes. In this case your example was all in the same translation unit which makes it trivial for the compiler to decide if it’s worth doing like that. (There are cases where it wouldn’t be worth it most likely and the compiler will have a pretty good set of heuristics to help it figure out what’s best for any given target).

    If they were in different translation units it becomes quite a lot harder to do. Some compilers will still manage the same optimisations but not all. You can of course ensure that it remains within the same translation unit for every case by defining functions like these as inline which lets you specify them in the header files.

    The moral of the story is don’t sweat over tiny details – writing code that makes sense and is maintainable is far more important.

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

Sidebar

Related Questions

consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
I tried the following code #include <stdio.h> int main(void) { typedef static int sint;
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
#include<stdio.h> #include<string.h> char *y; y=(char *)malloc(40); // gives an error here int main() {
can any one give me the dry output for this program? #include <stdio.h> main()
First is the code #include <stdio.h> typedef wchar_t* BSTR; wchar_t hello[] = LHello; class
Given this code: //header.h template <class T> class Foo { public: Foo(T t) :
Given such a code segment: #include <iostream> #include <iterator> #include <fstream> #include <string> using
I'd like to include the results of a given script (let's say a.php) into

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.