Solved

Class reference

Posted on 2013-06-13
3
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

622 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