Solved

Class reference

Posted on 2013-06-13
3
317 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 32

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 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

20 Experts available now in Live!

Get 1:1 Help Now