From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2922 invoked by alias); 12 Oct 2014 12:54:09 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 2909 invoked by uid 89); 12 Oct 2014 12:54:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-oi0-f46.google.com Received: from mail-oi0-f46.google.com (HELO mail-oi0-f46.google.com) (209.85.218.46) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Sun, 12 Oct 2014 12:54:07 +0000 Received: by mail-oi0-f46.google.com with SMTP id h136so10806979oig.33 for ; Sun, 12 Oct 2014 05:54:05 -0700 (PDT) X-Received: by 10.202.219.3 with SMTP id s3mr816797oig.88.1413118445316; Sun, 12 Oct 2014 05:54:05 -0700 (PDT) Received: from Deathwish.hagood ([74.221.202.8]) by mx.google.com with ESMTPSA id no6sm7677299obb.6.2014.10.12.05.54.04 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 12 Oct 2014 05:54:04 -0700 (PDT) Received: from [10.16.0.66] (chumley.hagood [10.16.0.66]) by Deathwish.hagood (Postfix) with ESMTP id E6072C7B8037; Sun, 12 Oct 2014 07:54:03 -0500 (CDT) Message-ID: <543A79EB.7040002@gmail.com> Date: Sun, 12 Oct 2014 12:54:00 -0000 From: David Hagood User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 MIME-Version: 1.0 To: gcc-help@gcc.gnu.org, Graziano.Servizi@bo.infn.it Subject: Re: Question References: <5433F137.8050008@bo.infn.it> In-Reply-To: <5433F137.8050008@bo.infn.it> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2014-10/txt/msg00078.txt.bz2 On 10/07/2014 08:57 AM, Graziano Servizi wrote: > So, if I have well understood I could write such a short code: > > # include > > class P > { > friend void friend_inline() > {std :: cout << P :: Name << ' ' << p << '\n';} You will need to qualify "p" as well: {std :: cout << P::Name << ' ' << P::p << '\n';} > static constexpr int Name = 20; > static constexpr double p = 12.5; > > }; > > int main() > { > P p; > // how can I call friend_inline ? friend_inline(); > } It's just a function. It's probably more clear if you split the definition of the function, the declaration of it being a friend, and the implementation: // Declaration: there exists a function named "friend_inline"... void friend_inline(); // Class declaration. class P { // Class P allows a non-class function "friend_inline" to access its private variables... friend void friend_inline(); static constexpr int Name = 20; static constexpr double p = 12.5; }; // Definition of the function "friend_inline" void friend_inline() { std::cout << P::Name << ' ' << P::p << std::endl; } Each of these things - declaration, friending, and definition - are separate things. They can be combined into one statement (as in your example) for brevity, but they still are 3 different things. Also: be VERY CAREFUL about using friend - it's almost always a bad idea. You are almost always better off making one or more public member functions, and calling them, than you are exposing every last private member to a non-member function. somewhat OT: I wouldn't put spaces between the class name, the colon, and the variable name in your code; they are all one thing - the name of the object being accessed. Also, use "std::endl" to end a sequence of insertions to a stream, not just '\n' - std::endl has more semantic meaning to the system, it really means "I am done with this line, do whatever you need to do, such as flushing it to the file", vs. "\n" which just means "insert a newline character".