Solved

Class reference

Posted on 2013-06-13
3
319 Views
Last Modified: 2013-06-28
I have a class defined as follows:
What is the use case of having a reference. it is so confusing :(

class Base
   public:
        class Inner {
              Base& _base;
              public:
                  Inner(Base& in);
         };
};

Base::Inner::Inner(Base& in) 
     : _base(in) {}

Open in new window

0
Comment
Question by:perlperl
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39245340
There could be a lot of reasons for such a construct, with the most likely one being to 'link' two specific objects together. Why that is done in your case is quite hard to tell from just that snippet. The must be a reason for 'Inner' to interact with a 'Base' object it is related to.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39245363
BTW, something more basic about references and how they are used: http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39247053
a reference member needs to be provided at construction time. so, contrary to a pointer member, the object referenced by the member has to exist at construction time of the Inner object (and would need to keep valid thru lifetime of the Inner object).  

as the Inner class is defined within Base class, it is even likely that the Base reference would be a reference to the own parent Base object.

class Base
{
   public:
        class Inner {
              Base& _base;
              public:
                  Inner(Base& in);
         };
        Base();
   private:
       Inner _inner;
};

Base::Inner::Inner(Base& in) 
     : _base(in) {}

Base::Base()
    : _inner(*this) {}

Open in new window


such a construction could be used for to encapsulate all major functionality in the Inner class and to offer only a minimal public interface for the class itself.

Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cb: unreferenced local variable 11 72
Compile GLUT with Visual Studio 2015 1 111
Error C2678: binary '!=': no operator found... 4 42
C++ mouse_event mouse look 7 70
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now