Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Class reference

Posted on 2013-06-13
3
Medium Priority
?
333 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 1500 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 35

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

715 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