Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C/C++, doubt on Friend function

C/C++, doubt on Friend function

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pankaj D Dubey
    wrote on last edited by
    #1

    A Friend function can be avoided if get()(to read) and set()(to write) member functions(public) are provided on (private) data members of a class. Is it True or False. Can anyone help me clearing this doubt with an appropriate reason?

    C E _ 3 Replies Last reply
    0
    • P Pankaj D Dubey

      A Friend function can be avoided if get()(to read) and set()(to write) member functions(public) are provided on (private) data members of a class. Is it True or False. Can anyone help me clearing this doubt with an appropriate reason?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The friend keyword allows you to specify functions or other classes which will be able to access all member (public, protected or private) of the class in which the friend keyword is used. Thus one way to avoid using it would be to provide getters and setters. But this is not always a good approach because all your members will become 'visible' to everybody, which is not always what you want.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • P Pankaj D Dubey

        A Friend function can be avoided if get()(to read) and set()(to write) member functions(public) are provided on (private) data members of a class. Is it True or False. Can anyone help me clearing this doubt with an appropriate reason?

        E Offline
        E Offline
        Eytukan
        wrote on last edited by
        #3

        dubeypankaj wrote:

        A Friend function can be avoided if get()(to read) and set()(to write) member functions(public) are provided on (private) data members of a class. Is it True or False.

        Well, you can do it with a friend function. You use getters-setters to access the read/write the private members. The same can be archived through friends but that would look raw.

        He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

        1 Reply Last reply
        0
        • P Pankaj D Dubey

          A Friend function can be avoided if get()(to read) and set()(to write) member functions(public) are provided on (private) data members of a class. Is it True or False. Can anyone help me clearing this doubt with an appropriate reason?

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          A friend function basically gives access to the internal data members of a class to an external function. This can definitely be achieved using getters and setters. But in reality friend functions are not used for this purpose. For example, if you look at the CStringT class, the overloaded + operator is declared as a friend function. This is so that the + operator can be used to concatenate two string in the following 2 ways

          CString cs1 = L"Hello";
          CString cs2 = L"World";
          CString cs3 = L"";

          cs3 = cs1 + L" World";
          cs3 = L"Hello " + cs2;

          If the + operator was an ordinary member function of the CStringT class, then only the first call would have been possible.

          «_Superman_» I love work. It gives me something to do between weekends.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups