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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:40:57+00:00 2026-05-19T04:40:57+00:00

Consider the following code: class A { private: class B {}; public: B f();

  • 0

Consider the following code:

class A
{
private:
    class B {};
public:
    B f();
};

A a;

A::B g()
{
    return a.f();
}

The compiler rejects this – g cannot return A::B because A::B is private.

But suppose I now use decltype to specify the return value of g:

class A
{
private:
    class B {};
public:
    B f();
};

A a;

decltype(a.f()) g()
{
    return a.f();
}

All of a sudden it compiles fine (with g++ >= 4.4).

So I’ve basically used decltype to get around an access specifier in a way I would not have been able to in C++98.

Is this intentional? Is this good practice?

  • 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-19T04:40:57+00:00Added an answer on May 19, 2026 at 4:40 am

    Access only applies to names (and as a special case, to constructors/destructors). It doesn’t apply to entities themselves. The spec further elaborates

    [ Note: because access control applies to names, if access control is applied to a typedef name, only the accessibility of the typedef name itself is considered. The accessibility of the entity referred to by the typedef is not considered. For example,

    class A {
      class B { };
    public:
      typedef B BB;
    };
    
    void f() {
      A::BB x; // OK, typedef name A::BB is public
      A::B y; // access error, A::B is private
    }
    

    — end note ]

    So what you have disovered here isn’t surprising. You can get hold of the type A::B even in C++03, when taking the address of f by saying &A::f and passing it to a function template deducing the return type.

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

Sidebar

Related Questions

Consider the following code in a DLL: public class ReceivingClass { private Assembly myAssembly;
consider the following code: public class SynchronizedCounter extends Thread { private int c =
Consider the following code: class CString { private: char* buff; size_t len; public: CString(const
Consider the following code: public class InboxMenuItemDynamicProvider : DynamicNodeProviderBase { private IMyService _myService {
Let's consider the following code: public class MyPanel extends JPanel { private long secretInfo
Consider the following code: CLASS AuditProgressReport : public class AuditProgressReport { private List<AuditProgressReport> audit_progress_reports
Consider the following code : public class RandomClass { private readonly string randomString; public
Consider the following code parts: this is Timing.h: class Timing { public: //creates a
Consider the following block of code: class CheckStore { private String displayText; private boolean
Consider the following piece of code - class MyThread extends Thread { private 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.