Thread: member vs non member function in C++

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    24

    member vs non member function in C++

    Hello,


    I am a newbie and learning C++ by myself from online. I was studying about the keyword Friend and here is the piece of sample code


    Code:
    class Myclass
    {
    	public:
    	Myclass()
    	{
    		reg =0;
    	};
    	private:
    	int reg ;
    	
    	friend void someFunc(Myclass &obj);
    }
    It said that declaring a non-member function as a friend of class allows it to access the class' private members


    So my question is that what is meant by non-member function Or specifically how somefunc become non-member function. Before reading this, I thought that the member function of a class is a function that has its definition or its prototype within the class definition. So here prototype of someFunc is defined that leads it to be a member function. Right?

  2. #2
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    Probably I find the answer by myself. The
    someFunc
    function is defined as a regular function outside the class that makes it Non-member function.

    The definition of
    someFunc
    is explain after the next page. I think it should be on the first page.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>friend void SomeFunc(Myclass &obj);
    This does not actually declare a function. This only tells the compiler that there is some function SomeFunc which should have access to the class's internals.

    The function still needs to be declared and defined outside the class or it won't work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This does not actually declare a function.
    O_o

    What makes you say that?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    This does not actually declare a function.
    It's visible to the class, even without another independent declaration in the global or enclosing namespace.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Lookup and declaration rules can be complicated. Not sure I understand them all...
    The friend declaration says it exists, but not within which namespace, which a function declaration should.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing member variable as default arg to member function
    By Memloop in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2009, 06:49 PM
  2. Replies: 3
    Last Post: 07-23-2006, 01:09 PM
  3. member member function pointers
    By Bigbio2002 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2005, 05:14 PM
  4. Function Pointers to a Member, from a Member
    By littleweseth in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2003, 02:12 AM
  5. private data member with public set member function
    By Mario in forum C++ Programming
    Replies: 2
    Last Post: 05-28-2002, 10:53 AM

Tags for this Thread