C++ Logo

std-discussion

Advanced search

Re: 回复: A Idea: Friend with Conditions

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 21 Jun 2025 13:56:21 -0400
On Sat, Jun 21, 2025 at 11:35 AM SD SH via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> > OK: *how* would it be useful? Why would you want a class to be a
> > friend just because that class has a default constructor, or is a base
> > class of something? Where would this stuff actually be useful?
> Just a example.
> Now imagine you are writing a program:
> a class named A, it has a lot of data.
> another class named Base, the A shares its data for Base and the derived classes from Base.

You're still just manufacturing an arbitrary set of conditions where
you declare that the problem exists, then providing a solution for it.
You're missing the point of what I asked for.

I want to know the *circumstances* under which "Base and the derived
classes from Base" would *need* to be able to access `A`s internals?
Why is it important that `A` break encapsulation in such an unbounded
way?

> > Also, what is `T` in the above example?
> Like template<typename T>.
> > Was there supposed to be a template header somewhere?
> You're right. The example above has conflicting syntax. But it doesn't matter, we can revise anytime.
>
> template<typename T>
> friend T if(...);
> is OK but I don't think is the best syntax.

You can't just make up identifiers in C++. We don't have template
header syntax because people like bulky syntax. We have it because we
need to tell the compiler that `T` means something and to some extent
*what* it means before we can actually use it in code somewhere.

Received on 2025-06-21 17:56:33