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. ATL / WTL / STL
  4. using friend

using friend

Scheduled Pinned Locked Moved ATL / WTL / STL
c++questioncsharpvisual-studiocom
8 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.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    Visual Studio 2008, c++ Class C_Messages works with various structures called messages. The initial configuration of the messages is by reading a text file and is rather extensive. C_Messages instantiates a class called C_Configuration to read the text file and perform all the startup work. Currently C_Messages instantiates C_Configuration then calls some functions handing C_Configuration pointers to the structures. Is there some way that C_Configuration can be declare a friend of C_Messages and dispense with the functions just to set the pointers. I am thinking of something like this in C_Messages.h

    Class C_Configuration;
    Class C_Messages
    {
    …
    Friend C_Configuration;
    int x;
    }

    Then in file C_Configuration.cpp would be something like:

    Class C_Messages
    Int Do_This()
    {
    X = 2;
    }

    When I read the various forum questions on this none seem to apply to a simple relationship like this. Maybe its just not possible so I am trying make a simple question out of this. EDIT I just figured out part of it and why the compiler is saying the variable is not static. If I don't want static variables can I do something like hand over the this pointer to C_Messages to C_Configuration then C_Configuration can access the members of C_Messages?

    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

    L T 2 Replies Last reply
    0
    • B bkelly13

      Visual Studio 2008, c++ Class C_Messages works with various structures called messages. The initial configuration of the messages is by reading a text file and is rather extensive. C_Messages instantiates a class called C_Configuration to read the text file and perform all the startup work. Currently C_Messages instantiates C_Configuration then calls some functions handing C_Configuration pointers to the structures. Is there some way that C_Configuration can be declare a friend of C_Messages and dispense with the functions just to set the pointers. I am thinking of something like this in C_Messages.h

      Class C_Configuration;
      Class C_Messages
      {
      …
      Friend C_Configuration;
      int x;
      }

      Then in file C_Configuration.cpp would be something like:

      Class C_Messages
      Int Do_This()
      {
      X = 2;
      }

      When I read the various forum questions on this none seem to apply to a simple relationship like this. Maybe its just not possible so I am trying make a simple question out of this. EDIT I just figured out part of it and why the compiler is saying the variable is not static. If I don't want static variables can I do something like hand over the this pointer to C_Messages to C_Configuration then C_Configuration can access the members of C_Messages?

      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What you have shown above doesn't really make sense. The use of the friend keyword is to allow access to protected or private variables of the other class, but you still need an instance of the class to access them. It's most useful when the friend class inherits the other one. If you are just interested in accessing the properties or methods of an object of another class, then you should make them public.

      B 1 Reply Last reply
      0
      • L Lost User

        What you have shown above doesn't really make sense. The use of the friend keyword is to allow access to protected or private variables of the other class, but you still need an instance of the class to access them. It's most useful when the friend class inherits the other one. If you are just interested in accessing the properties or methods of an object of another class, then you should make them public.

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        Re:

        Quote:

        The use of the friend keyword is to allow access to protected or private variables of the other class

        That is the part I thought I understood.

        Quote:

        but you still need an instance of the class to access them.

        That is what I have now figured out, but I think only partially.

        Quote:

        It's most useful when the friend class inherits the other one

        Is the intent most useful or only useful. I am thinking only. I was thinking friend had a rather broader scope. Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work. Other than for startup (which requires some 80,000+ data items from a file), nothing in M needs to be public. But this entire app is and always be run in a closed system with strictly controlled ins and outs. I am leery of public variables but that would probably better than sending over a flock of pointers. Does the use of public seem to be my best option.

        Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

        L A 2 Replies Last reply
        0
        • B bkelly13

          Re:

          Quote:

          The use of the friend keyword is to allow access to protected or private variables of the other class

          That is the part I thought I understood.

          Quote:

          but you still need an instance of the class to access them.

          That is what I have now figured out, but I think only partially.

          Quote:

          It's most useful when the friend class inherits the other one

          Is the intent most useful or only useful. I am thinking only. I was thinking friend had a rather broader scope. Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work. Other than for startup (which requires some 80,000+ data items from a file), nothing in M needs to be public. But this entire app is and always be run in a closed system with strictly controlled ins and outs. I am leery of public variables but that would probably better than sending over a flock of pointers. Does the use of public seem to be my best option.

          Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          bkelly13 wrote:

          Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work.

          Then there is no need for them to be friends.

          bkelly13 wrote:

          I am leery of public variables

          You can hid the variables by making them private, and creating public methods to get or set them, which is the generally recommended pattern in OOP. At the end of the day it all depends on what each of your classes is supposed to do.

          1 Reply Last reply
          0
          • B bkelly13

            Re:

            Quote:

            The use of the friend keyword is to allow access to protected or private variables of the other class

            That is the part I thought I understood.

            Quote:

            but you still need an instance of the class to access them.

            That is what I have now figured out, but I think only partially.

            Quote:

            It's most useful when the friend class inherits the other one

            Is the intent most useful or only useful. I am thinking only. I was thinking friend had a rather broader scope. Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work. Other than for startup (which requires some 80,000+ data items from a file), nothing in M needs to be public. But this entire app is and always be run in a closed system with strictly controlled ins and outs. I am leery of public variables but that would probably better than sending over a flock of pointers. Does the use of public seem to be my best option.

            Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            If all instances of C are owned by and created by an instance of M and there are no other clients of C then I can't see a lot of problem in making M a friend of C. You're essentially saying that C is a part of M you don't need all the time so you want to tear it off and discard it after some initialisation process. Another way of doing it would be to introduce a new class, A that contains the bits of M that C needs to manipulate. M would contain an instance of A and pass a pointer to the instance to C when needed. That gives you a bit more future proofing and decoupling - both C and M don't depend on each other and just depend on A. Yet another way would be to have a getter/setter interface on M but if C's the only thing that would want to use it then you're complicating the interface of M for no real reason. Personally I'd go for the first and consider the second or third later if the requirements change and you need more flexibility.

            B 1 Reply Last reply
            0
            • A Aescleal

              If all instances of C are owned by and created by an instance of M and there are no other clients of C then I can't see a lot of problem in making M a friend of C. You're essentially saying that C is a part of M you don't need all the time so you want to tear it off and discard it after some initialisation process. Another way of doing it would be to introduce a new class, A that contains the bits of M that C needs to manipulate. M would contain an instance of A and pass a pointer to the instance to C when needed. That gives you a bit more future proofing and decoupling - both C and M don't depend on each other and just depend on A. Yet another way would be to have a getter/setter interface on M but if C's the only thing that would want to use it then you're complicating the interface of M for no real reason. Personally I'd go for the first and consider the second or third later if the requirements change and you need more flexibility.

              B Offline
              B Offline
              bkelly13
              wrote on last edited by
              #6

              First option was:

              Quote:

              then I can't see a lot of problem in making M a friend of C.

              I was thinking that C would be a friend of M allowing C to see into M. Am I thinking wrong on that. What is the syntax to make this declaration? C gets access to the variables of M, can change them, then C gets deleted.

              Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

              A 1 Reply Last reply
              0
              • B bkelly13

                First option was:

                Quote:

                then I can't see a lot of problem in making M a friend of C.

                I was thinking that C would be a friend of M allowing C to see into M. Am I thinking wrong on that. What is the syntax to make this declaration? C gets access to the variables of M, can change them, then C gets deleted.

                Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                You're right - I got things a bit bum backwards. Basically you want C to be able to look at the implementation details of M so you declare C a friend in M:

                class M
                {
                // Somewhere in the declaration of M...
                friend class C;
                };

                should do the trick.

                1 Reply Last reply
                0
                • B bkelly13

                  Visual Studio 2008, c++ Class C_Messages works with various structures called messages. The initial configuration of the messages is by reading a text file and is rather extensive. C_Messages instantiates a class called C_Configuration to read the text file and perform all the startup work. Currently C_Messages instantiates C_Configuration then calls some functions handing C_Configuration pointers to the structures. Is there some way that C_Configuration can be declare a friend of C_Messages and dispense with the functions just to set the pointers. I am thinking of something like this in C_Messages.h

                  Class C_Configuration;
                  Class C_Messages
                  {
                  …
                  Friend C_Configuration;
                  int x;
                  }

                  Then in file C_Configuration.cpp would be something like:

                  Class C_Messages
                  Int Do_This()
                  {
                  X = 2;
                  }

                  When I read the various forum questions on this none seem to apply to a simple relationship like this. Maybe its just not possible so I am trying make a simple question out of this. EDIT I just figured out part of it and why the compiler is saying the variable is not static. If I don't want static variables can I do something like hand over the this pointer to C_Messages to C_Configuration then C_Configuration can access the members of C_Messages?

                  Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                  T Offline
                  T Offline
                  Theo Buys
                  wrote on last edited by
                  #8

                  friend can be used to give another class access to the protected and private members of the class that specified friend. If C_Messages want to access the protected and private members of the class C_Configuration then C_Configuration specified C_Messages as friend. If you aggregate the class C_Messages so that is has a C_Configuration, you can only access the public members of C_Configuration in C_Messages. But with the friend specifier you can also access the protected and private members. Sample code:

                  class C2;
                  class C1
                  {
                  public:
                  int public_var;
                  protected:
                  int protected_var;
                  private:
                  int private_var;
                  friend class C2;
                  };

                  class C2
                  {
                  public:
                  class C1 member;
                  void set_private_var (int v) { member.private_var = v; }
                  void set_protected_var (int v) { member.protected_var = v; }
                  };

                  void Test()
                  {
                  C2 obj;

                  obj.member.public\_var = 100; // ok
                  
                  // ERROR: can't access protected and private member
                  // obj.member.protected\_var = 100; 
                  // obj.member.private\_var = 200; 
                  
                  obj.set\_protected\_var(100); // ok
                  obj.set\_private\_var(200);   // ok
                  

                  }

                  Another solution is working with an intermediate class which specifies the friend. In this case you get only access to public and protected members of the base class. Sample code:

                  class C1
                  {
                  public:
                  int public_var;
                  protected:
                  int protected_var;
                  private:
                  int private_var;
                  };

                  class C1B : public C1
                  {
                  friend C2;
                  }

                  class C2
                  {
                  public:
                  class C1B member;
                  void set_protected_var (int v) { member.protected_var = v; }
                  };

                  void Test()
                  {
                  C2 obj;

                  obj.member.public\_var = 100; // ok
                  
                  // ERROR: can't access protected and private member
                  // obj.member.protected\_var = 100; 
                  // obj.member.private\_var = 200; 
                  
                  obj.set\_protected\_var(100); // ok	
                  

                  }

                  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