#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();
};
Pass a class by referencePassing by reference
Page 1 of 1
3 Replies - 11009 Views - Last Post: 08 February 2009 - 11:23 AM
#1
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?
Replies To: Pass a class by reference
#2
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:
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
#3
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 constructorDictionary my_dictionary; Controller my_controller( my_dictionary );
This post has been edited by Bench: 08 February 2009 - 11:04 AM
#4
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.
Hope this helps!
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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|