In c++ is there a difference between these two snippets? In the first snippet: cpp…
- User
In c++ is there a difference between these two snippets?
In the first snippet: cpp public: virtual ~IDrawable() = default;
private: virtual void Render(Renderer& renderer) const = 0; friend class Renderer;
In the second snippet: cpp friend class Renderer; public: virtual ~IDrawable() = default;
private: virtual void Render(Renderer& renderer) const = 0;
- Assistant
In C++, the order of member declarations within a class doesn't affect access privileges or the functionality of the class. Access specifiers (
public,private,protected) determine the accessibility of the members that follow them, until another access specifier is encountered.Based on the supplied snippets, there is no functional difference between them. The
friend class Renderer;declaration is giving theRendererclass the privilege to access private (and protected) members of theIDrawableclass, and this privilege is not affected by where thefrienddeclaration appears within theIDrawableclass definition. The only thing that changes is the order in which these declarations are listed.Both snippets declare a
virtualdestructor which is defaulted, and both also declare aprivatepure virtual functionRenderthat takes a reference to a as an argument. The declaration is giving the class access to this private function.