Friend and templates

This is a discussion on Friend and templates within the C++ Programming forums, part of the General Programming Boards category; 1. What does actually a friend (keyword) do (isnīt a good friend to me :-) )? I think it has ...

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Friend and templates

    1. What does actually a friend (keyword) do (isnīt a good friend to me :-) )? I think it has something to do with classes and functions??? Have found some examples but I really cannot grasp it.

    2. I have a function that returns a template
    Code:
    template <class T> Person<T> CreateAPersonTemplate()
    {
    Person<int> Steve(35);
    return Steve;
    }
    
    //Also a class Person
    template <class T> class Person
    {
    public:
    Person(T age) {itsAge=age;}
    ~Person(){}
    void SetAge(T age) {itsAge = age;}
    T GetAge() const {return itsAge;}
    private:
    T itsAge;
    };
    My problem is when a call the function, CreateAPersontemplate<int>(). Why cannot I just call CreateAPersonTemplate()??? Why do I have to tell the compiler which type Iīm sending? The type is defined in the function.

    Thx in Advance

  2. #2
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    1. What does actually a friend (keyword) do (isnīt a good friend to me :-) )? I think it has something to do with classes and functions??? Have found some examples but I really cannot grasp it.
    With friends you are able to grant access to your classes members without making them public.
    My problem is when a call the function, CreateAPersontemplate<int>(). Why cannot I just call CreateAPersonTemplate()??? Why do I have to tell the compiler which type Iīm sending? The type is defined in the function.
    You are using templates, that's why. If you know which type you are using then why do you use templates?

    If you want to call it like CreateAPersonTemplate(), then you have to define the function as:
    Code:
    Person<int> CreateAPersonTemplate()
    {
            Person<int> Steve(35);
            return Steve;
    }

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Of course, where is my head , dahhhh ^^.

    Iīve tried to to use friends but apparently Iīm doing something wrong because it doesnīt work!!!. Maybe you can tell whats wrong.

    Code:
    class Test1
    {
    public:
    private:
    int number;
    }
    
    class Test2 : public Test1
    {
    public:
    friend GetNumber() const {return number;}
    friend SetNumber(int numb) {number = numb;}
    }
    When a call GetNumber() or SetNumber() I get a compile error.

    I know that this isnīt the perfect example but I only want to see how it works.

    Thx in Advance

  4. #4
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Code:
    class Test1
    {
    public:
    private:
    int number;
    }
    class Test2 : public Test1
    {
    public:
    friend GetNumber() const {return number;}
    friend SetNumber(int numb) {number = numb;}
    }
    The friend keyword grants access, it doesn't get them

    If you want class Test2 to access Test1's private variables, you had better tell class Test1 that Test2 is a friend of it.
    Code:
    class Test1
    {
    public:
    private:
      int number;
      friend class Test2;
    };
    
    class Test2 : public Test1
    {
    public:
      int GetNumber() const {return number;}
      void SetNumber(int numb) {number = numb;}
    };
    But note that you could define the variable number protected. Then you don't need to make class Test2 Test1's friend because it inherits it.
    Last edited by raimo; 07-05-2002 at 01:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 06:50 PM
  2. Replies: 8
    Last Post: 07-24-2006, 09:14 AM
  3. friend function in templates
    By arjunajay in forum C++ Programming
    Replies: 0
    Last Post: 01-22-2006, 04:32 AM
  4. help with templates
    By drdodirty2002 in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2004, 06:25 AM
  5. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 05:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21