views:

96

answers:

5

I'm creating a Class.

This class stores user preferences in a Struct.

When creating an instance of the class, I want the client to have the option of creating an instance with no preferences passed in or with a preferences struct passed in.

I can do this with pointers, but I wanted to know how I could do it by passing the preferences struct into the class by reference.

Either way, once the class receives the preferences it makes a copy for its own use.

Here's what it looks like with pointers

struct preferences {};
class Useful 
{
public:
    Useful(preferences const * = NULL);
...
}

...

int main() 
{
   preferences * testPrefs;
   ...
   Useful testClass(testPrefs);
   // or if no prefs: Useful testClass;
   ...
}

So how would you pass the preferences struct in by reference when creating an instance of the class with a default value of no struct passed in? This is the line I'm stuck on, since neither NULL nor *NULL will work:

class Useful 
{
public:
    Useful(preferences & = ???????);
+3  A: 

Create a static instance of the preferences struct with default values filled in, and use that as the default parameter. Or fill it with "magic" values that indicate it as a special reserved value.

Mark Ransom
+4  A: 

You point out the advantage of pointers over references, and how well pointers fit your situation, then announce you don't want to use them.

You can still get what you want. Write two overloads for your constructor. One takes a reference, the other takes no parameters and does what the other constructor did when it got a null pointer.

Kate Gregory
Well, I wanted to know how to do it. In this case pointers may work well, but in another case, passing by reference may work better.
Peter Ajtai
Peter Ajtai
+7  A: 
Useful(const preferences& p = preferences(...));
Pavel Minaev
Nice. I'll have to remember that this is a possibility.
Peter Ajtai
+1  A: 

You must take care about who will be the owner of the "preferences" pointer, if you transfer the ownership the class must delete it in the destructor, if not, the caller must destroy it.

because this could give you lot of headaches I suggest use the reference pass parameters.

and as say Kate Gregory, you must define two constructors, in the 2nd you must copy the preferences to your own instance, at least that preference are only one and static set.

with an unique and static "preferences" you will have the trouble that other section of the code modified it and alter how your code works.

Gustavo V
+2  A: 

You could simply overload the constructor:

struct preferences {};
class Useful 
{
public:
    Useful(preferences&);
    Useful(); // In here, do whatever you would have done 
              // if preferences were NULL in the pointer example
}

That's essentially what the first version of the code with the pointer and default argument is doing anyway, behind the scenes.

Tyler McHenry