Thread: Few C++ questions

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    Few C++ questions

    hey guys! I have a few questions that came arcoss as I learn C++.
    this is going to be a batch of questions so please be patient, help will be appritiated !
    Delegation:
    the idea of delegation is a but abscure for me.
    as far as I understand - delegation is giving an object the functionality of other object, because inheritance is not right enough for that task.
    (correct me if I wrong please)
    delegation is used in many ways, but 2 main ways of doing it is by:
    1) containing an object in another object , and call the contained-object-methods to the conatining-object
    2) by using pointers to function. this require a small explanation - do I create a pointer to a member function and set it to be as a member in other object?

    Frienship:
    why can't this code be compiled?
    Code:
    #include <iostream>
    
    class A{
    private:
        friend class B;
        int x;
    public:    
        A(int r) :x(r) {};
    };
    
    class B{
        public:
        void print (int y){std::cout<<y;}
    };
    
    int main (void){
        A a(6);
        B b;
        b.print(a.x);    
        return 0;
    }
    it's complaining that A::x is private - but the frienship should give B excess to all of A members, right?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    But B isn't accessing A::x. main() is. You'd have to make main() a friend of A if you want to do this.
    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?

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    I see, thanks!
    what about the other questions?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Delegation can be done a number of ways. Function pointers are but one. You can use boost::bind/std::bind, or you can use a lambda function, or even a functor. I suggest you google each of those.
    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?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The idea behind delegation is some piece of code A calls some other piece of code B, but the connection between A and B is made at runtime instead of at compile time. In other words, code B might change from one call to the next.

    In your case 1, it is not really delegation because the "contained object" is (presumably) defined at compile time by the source code. So it's not delegation because you know exactly what method will be called even without running the program.

    In your case 2, this is delegation, because the function pointer could point at ANY function with the proper method signature. So the target of the call can in principle change at runtime, and you cannot tell simply by looking at the source where the call will go.

    As far as your friend problem... Look at your code again. The expression "a.x" is being evaluated by main() -- NOT by the B class. So it is main() that would have to be a friend of A for this code to compile.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A few questions
    By beene in forum Game Programming
    Replies: 9
    Last Post: 04-18-2007, 10:11 AM
  3. Two C Questions Help?
    By The_Lone_Wolf in forum C Programming
    Replies: 2
    Last Post: 11-25-2005, 06:52 AM
  4. SDL questions.
    By antex in forum Game Programming
    Replies: 3
    Last Post: 10-30-2005, 12:08 AM
  5. Two Questions
    By BlahBlahBlah in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2002, 02:24 AM