C++ Logo

std-discussion

Advanced search

Re: 回复: A Idea: Friend with Conditions

From: SD SH <Z5515zwy_at_[hidden]>
Date: Sat, 21 Jun 2025 19:28:55 +0000
> reason?
I'm writing a non-existent syntax features of C++.
Simply put it is that
the real owner of the members of <class A> is <class B>
and derived classes from <B> can also access it.

> meaning of template header.
Right. I know it. I just think the number of template paraments is at most 1 (may be, I think so). The earliest syntax has problem.
获取Outlook for Android<https://aka.ms/AAb9ysg>

________________________________
发件人: Std-Discussion <std-discussion-bounces_at_[hidden]> 代表 Jason McKesson via Std-Discussion <std-discussion_at_[hidden]>
发送时间: 星期日, 六月 22, 2025 1:56:51 上午
收件人: std-discussion_at_[hidden]p.org <std-discussion_at_[hidden]>
抄送: Jason McKesson <jmckesson_at_[hidden]>
主题: Re: [std-discussion] 回复: A Idea: Friend with Conditions

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.
--
Std-Discussion mailing list
Std-Discussion_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2025-06-21 19:29:01