Pass a class by reference

Passing by reference

Page 1 of 1

3 Replies - 16252 Views - Last Post: 08 February 2009 - 11:23 AM Rate Topic: -----

#1 Scorpiobuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-February 09

Pass a class by reference

Posted 08 February 2009 - 08:58 AM

If I want to pass the Dictionary class into this class by reference, I assumed you simply instantiated the object of the class with the & symbol and everything else was fine, but apparently you have to do something with the constructor and that is where my question lies. Right now, everything in the code has a default constructor, nothing fancy. How do I go about moving a class in by reference?


#include Menu.h //class with def constructor
#include Dictionary.h // class with def constructor

class Controller 
{
Menu conMenu; //class passed in using composition
Dictionary dict; //class passed in using composition
void start(); //function that prints out the first menu
char userOpt; //to return the user's menu choice
void loadDict(); // function that loads a number of text files
Controller();
};




Is This A Good Question/Topic? 0
  • +

Replies To: Pass a class by reference

#2 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Re: Pass a class by reference

Posted 08 February 2009 - 10:46 AM

Just declare it like this:

Dictionary* dict;

This will give you a pointer to a dictionary object called dict. It's how you pass this object between functions with a reference symbol (&) that allows you to pass by reference and not by value.

If you want to pass it into the class using the constructor, then it'll look something like this:

// constructor
Controller (Dictionary* dict);

// then wherever you declare your Controller object,
// you'll have to pass in a dictionary object like so:
Dictionary* dict;
Controller theController(&dict);


This post has been edited by Psionics: 08 February 2009 - 10:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Pass a class by reference

Posted 08 February 2009 - 10:59 AM

you can store references to objects in your class - you just need to use the constructor's initialiser list to assign them. references aren't allowed to be 'empty', so they must be assigned something before the body of a constructor starts, and this is exactly what an initialiser list does.
class Controller
{
    Dictionary& dict;
public:
    Controller(Dictionary& ref) : dict(ref) {}
}; 
the initialiser list is a list of member variables after the : (in this case, just the one - a reference variable called dict), which gives your member variables their initial state/value. With a Controller class like this, you could instantiate a Controller object by passing a Dictionary to the constructor
Dictionary my_dictionary;
Controller my_controller( my_dictionary ); 

This post has been edited by Bench: 08 February 2009 - 11:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Pass a class by reference

Posted 08 February 2009 - 11:23 AM

Also...

You included your header files wrong. You don't include custom headers like others. You have to put quotes around 'em.

#include "Menu.h"
#include "Dictionary.h"


Hope this helps! :)

This post has been edited by Locke: 08 February 2009 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1