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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:59:47+00:00 2026-06-07T19:59:47+00:00

Why do classes in C++ have to declare their private functions? Has it actual

  • 0

Why do classes in C++ have to declare their private functions? Has it actual technical reasons (what is its role at compile time) or is it simply for consistency’s sake?

  • 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-07T19:59:49+00:00Added an answer on June 7, 2026 at 7:59 pm

    I asked why private functions had to be declared at all, as they don’t add anything (neither object size nor vtable entry) for other translation units to know

    If you think about it, this is similar to declaring some functions static in a file. It’s not visible from the outside, but it is important for the compiler itself. The compiler wants to know the signature of the function before it can use it. That’s why you declare functions in the first place. Remember that C++ compilers are one pass, which means everything has to be declared before it is used.1

    From the programmer’s point of view, declaring private functions is still not completely useless. Imagine 2 classes, one of which is the friend of the other. The friendzoned class2 would need to know how the privates of that class look like, (This discussion is getting weird) otherwise they can’t use it.

    As to why exactly C++ was designed in this way, I would first say there is the historical reason: the fact that you can’t slice a struct in C, was adopted by C++ so you can’t slice a class (and adopted by other languages branched from C++, too). I’d also guess that it’s about simplicity: Imagine how difficult it would be to devise a method of compilation in which you can split the class among different header files, let your source files know about it, and prevent others from adding stuff to your class.

    A final note is that, private functions can affect vtable size. That is, if they are virtual.


    1 Actually not entirely. If you have inline functions in the class, they can refer to functions later defined in the same class. But probably the idea started from single pass and this exception later added to it.

    2 It’s inlined member functions in particular.

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

Sidebar

Related Questions

Is there any way to detect when classes have changed during run-time with Hotswap?
I have classes A, B and C. A has members B and C. C
I have 2 classes that have exact two functions. The difference between them is
For object instances we can have their class declare some protocol conformance as in:
Abstract classes and interfaces play very a important role in Java and they have
I usually declare my classes and templates, and then define their methods after (in
Say I have the following classes in C++, and I want to inspect their
I have one interface that contains four functions. I have about 20 classes that
Ok I have two modules, each containing a class, the problem is their classes
I sometimes declare classes in nested namespaces and when it comes to defining their

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.