user1631224
user1631224

Reputation: 409

How to use Nested Classes properly in C++?

I am learning C++, and I'm trying to learn more about using the friend keyboard.

However, I am having trouble using nested classes in my Header file.

I know that header files should only be used for declarations but I didnt want to include a cpp file with it so I just used a header file to declare and build.

Anways, I have a main.cpp file that I want strictly to be used for creating objects of classes and accessing its functions.

However, I dont know exactly how to create the FriendFunctionTest function in my header file to where I can access it in my main.cpp source file using the header Class object because I'm trying to understand the "friend" keyword.

Here is my header code:

#ifndef FRIENDKEYWORD_H_
#define FRIENDKEYWORD_H_

using namespace std;

class FriendKeyword
{
    public:
        FriendKeyword()
        {//default constructor setting private variable to 0
            friendVar = 0;
        }
    private:
        int friendVar;

    //keyword "friend" will allow function to access private members
    //of  FriendKeyword class
    //Also using & in front of object to "reference" the object, if
    //using the object itself, a copy of the object will be created
    //instead of a "reference" to the object, i.e. the object itself
    friend void FriendFunctionTest(FriendKeyword &friendObj);
};

void FriendFunctionTest(FriendKeyword &friendObj)
{//accessing the private member in the FriendKeyword class
    friendObj.friendVar = 17;

    cout << friendObj.friendVar << endl;
}

#endif /* FRIENDKEYWORD_H_ */

In my main.cpp file, I wanted to do something like this:

FriendKeyword keyObj1;
FriendKeyword keyObj2;
keyObj1.FriendFunctionTest(keyObj2);

But obviously its not going to work since the main.cpp cant find the FriendFunctionTest function in the header file.

How do I fix this issue?

And I apologize again, I'm just trying to learn C++ online.

Upvotes: 0

Views: 695

Answers (3)

Jive Dadson
Jive Dadson

Reputation: 17016

A friend is not a member. Here is a good example of how "friend" is used in practice.

namespace Bar {

class Foo {

    public:
       // whatever...

    friend void swap(Foo &left, Foo &right); // Declare that the non-member function
                                             // swap has access to private section.
    private:
       Obj1  o1;
       Obj2 o2;
 };

 void swap(Foo &left, Foo &right) {
     std::swap(left.o1, right.o1);
     std::swap(left.o2, right.o2);
 }

} // end namespace Bar

We have declared a function for swapping Foo's that is more efficient than std::swap would be, assuming that classes Obj1 and Obj2 have efficient move-semantics. (Darn it, you are quick with that green check mark! :))

It is useful to know that because the swap routine is parameterized by a Foo object (two in this case) and is declared in the same namespace as Foo, it becomes part of Foo's public interface, even though it is not a member. The mechanism is called "argument-dependent lookup" (ADL).

Upvotes: 0

paulsm4
paulsm4

Reputation: 121599

You're really talking about several completely different things here. Here are examples for two of them:

1) "Friends":

  • http://www.learncpp.com/cpp-tutorial/813-friend-functions-and-classes/

    // Class declaration class Accumulator { private: int m_nValue; public: Accumulator() { m_nValue = 0; } void Add(int nValue) { m_nValue += nValue; }

    // Make the Reset() function a friend of this class friend void Reset(Accumulator &cAccumulator); };

    // Reset() is now a friend of the Accumulator class void Reset(Accumulator &cAccumulator) { // And can access the private data of Accumulator objects cAccumulator.m_nValue = 0; }

2) "Nested classes":

Upvotes: 0

willkill07
willkill07

Reputation: 142

The friend keyword is only used to specify if a function or other class can have access to the private members of that class. You have no need for class inheritance or nesting because FriendFunctionTest is a global function. Global functions do not require any class prefixes when invoked.

Source for friend: http://msdn.microsoft.com/en-us/library/465sdshe(v=vs.80).aspx

Upvotes: 1

Related Questions