-1

So I recently just learned about the friend and this in C++, I was watching a tutorial for beginners in C++ and programming. I was interested in this syntax or whatever this is and he said that it was a pointer and stores the object's address so I experimented with it.

Btw is it possible to use a different class object from a different class function? If so, how?

Anyways here is the code

||
\/

#include <iostream>

    class A
    {
    public:
        void Aprint()
        {
            std::cout << "It is A " << this->Number << std::endl;
        }
    private:
        int Number = 1;
    };

    class B
    {
    public:
        void Bprint()
        {
            std::cout << "It is B " << std::endl;
        }
    private:
        int Number = 0;

        friend void A::Aprint();
    };

    int main()
    {
       A Abo;
       B Bbo;

       Abo.Aprint();
    }

I want it to print 0 when I use a B class object. Like show 0 after "It is A" when it is called or when compiled. Cause I want to see what will happen when I use Bbo.Aprint(). I want to know how this and friend works. Still experimenting tho.

Before it was `Bbo.Aprint()` just edited.
  • 2
    Of course it should give an error. The `B` class doesn't have a member function named `Aprint`. That's not how `friend` works. Please [get a couple of good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), or reread the books or tutorials you're already reading, or go through your class-notes again. – Some programmer dude Feb 13 '19 at 13:51
  • I saw someone do it so I tried it. He didn't use the (this) with his program but somehow it worked. Ask him I guess. I watched it over and over and just followed his steps – ImmaNoobInProgramming Feb 13 '19 at 13:53
  • Could it have something to do with inheritance and virtual functions and polymorphism? – Some programmer dude Feb 13 '19 at 13:57
  • And please don't change your question to much, now my first comment (and worse, the posted answer!) doesn't match the code. – Some programmer dude Feb 13 '19 at 13:58
  • Alright I'll post a note saying its edited so it will match. – ImmaNoobInProgramming Feb 13 '19 at 14:01
  • Please elaborate more on `I want it to print 0 when I use a B class object.` I can't see any usage of B object here now. – Kunal Puri Feb 13 '19 at 14:03

3 Answers3

2

I think you re trying to imitate inheritance with friend declaration. As far as i understand friend declarations allows you to access private members of class A from a friend class or fuction. If you want your class B to be able to call class A functions i think you should use inheritance and virtual functions.

Also maybe this will help you.

https://www.ibm.com/support/knowledgecenter/SS2LWA_12.1.0/com.ibm.xlcpp121.bg.doc/language_ref/cplr042.html

com
  • 71
  • 7
1

You cannot call a member function of one class using an instance of another class (unless the classes are related by inheritance):

Abo.Aprint(); // OK
Bbo.Aprint(); // Not OK
eerorika
  • 232,697
  • 12
  • 197
  • 326
1

There is one way in which you can do that. For that, you have to change the signature of A::Aprint to void Aprint(const B&);

#include <iostream>

class B; // forward declaration

class A
{
    public:
    void Aprint(const B&);
    private:
    int Number = 1;
};

class B
{
    public:
    void Bprint()
    {
        std::cout << "It is B " << std::endl;
    }
    private:
    int Number = 0;

    friend void A::Aprint(const B&);
};

void A::Aprint(const B& b) {
    std::cout << "It is A " << b.Number << std::endl;
}

int main()
{
    A Abo;
    B Bbo;

    Abo.Aprint(Bbo);
}

In this example, Because A::Aprint() is friend of B, Aprint() can access even the private members of Bbo (See b.Number works even if it is private)

Kunal Puri
  • 3,419
  • 1
  • 10
  • 22