Declare a reference and initialize later?
You need to initliaze it. But if you would like to conditionally initialize it, you can do something like this:
MyObject& ref = (condition) ? MyObject([something]) : MyObject([something else]);
Is it possible to initialize reference variables later?
In short: No, it's intentionally not possible.
Think twice: Something like uninitialized references cannot really exist; such wouldn't make sense at all.
Thus they'll need to be set at the time of construction of the enclosing class, or at a point of static initialization.
You'll need to use pointers for such case.
Besides note that
yumminess = (int*)view;
would be wrongly casted (to a pointer) anyway.
"Right now i just use a pointer and dereference it all the time ..."
That's also easy to overcome writing an appropriate member function to access the reference.
int* yumminess;
// ...
int& yumminessRef() {
if(!yumminess) {
throw some_appropriate_exception("`yumminess` not initialized properly.");
}
return *yumminess;
}
Initializing reference variables in initialization list
guesser
and provider
need to be declared inside the class Game { }
scope if they are to be initialized by the constructor.
You can't initialize a reference to a class object by providing arguments to the constructor of its class, though. References must be initialized by a preexisting object. (Non-member references can also be initialized by temporary objects, but that's a bit different.)
Also, reference members are often a red flag for class design. You might want to use regular member objects for guesser
and provider
instead of references.
class Game {
Guesser guesser;
Provider provider;
};
Initialization of reference member requires a temporary variable
A reference must be initialised to refer to something; it can't refer to nothing, so you can't default-construct a class that contains one (unless, as others suggest, you define a global "null" value). You will need a constructor that is given the Div
to refer to:
explicit A(Div &d) : divs(d) {}
If you want it to be able to be "null", then you need a pointer, not a reference.
How to initialize the reference member variable of a class?
That is because references can only be initialized in the initializer list. Use
Test (int &x) : t(x) {}
To explain: The reference can only be set once, the place where this happens is the initializer list. After that is done, you can not set the reference, but only assign values to the referenced instance. Your code means, you tried to assign something to a referenced instance but the reference was never initialized, hence it's not referencing any instance of int
and you get the error.
Initializing a reference to member to NULL in C++
No, references cannot be NULL
in C++.1
Possible solutions include:
- using a pointer instead of a reference.
- having a dummy
Object
instance that can be used to indicate "no object".
[1] From the C++11 standard:
[dcl.ref] [...] a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.
Reference member variable initialization error with default constructor
why defining a default constructor throws compilation error?
It's not that you define a default constructor, it's that the default constructor's definition doesn't initialize i
. You are required to initialize all member variables that are references, and your empty definition does not do that.
This is for the same reason that you are required to initialize reference variables:
void foo() {
int &i; // error: declaration of reference variable 'i' requires an initializer
}
why would the compiler allow declaration of default constructor(without definition)
Because the definition is the problem, not the declaration. For example, moving the ill-formed constructor definition outside of the class definition will yield the same error:
class SomeClass
{
public:
SomeClass();
int &i;
};
SomeClass::SomeClass() {} // error: constructor for 'SomeClass' must explicitly initialize the reference member 'i'
The only problem with both examples is that you're not initializing i
.
Take note of the following examples, which will compile. Note that the constructor's declaration does not change, but the definition does initialize i
.
int someGlobalInt;
class SomeClass
{
public:
SomeClass() : i(someGlobalInt) {}
int &i;
};
int someGlobalInt;
class SomeClass
{
public:
SomeClass();
int &i;
};
SomeClass::SomeClass() : i(someGlobalInt) {}
Related Topics
Why Does C++ Not Have a Const Constructor
How to Redirect Output to a File with Createprocess
Inaccessible Direct Base' Caused by Multiple Inheritance
"Observable Behaviour" and Compiler Freedom to Eliminate/Transform Pieces C++ Code
Correct Printf Format Specifier for Size_T: %Zu or %Iu
Undefined Reference to '_Stack_Chk_Fail'
Will C++ Exceptions Safely Propagate Through C Code
What Happens When You Close a C++ Console Application
Rationale of Enforcing Some Operators to Be Members
Is the Size of a Struct Required to Be an Exact Multiple of the Alignment of That Struct
Std::Vector Iterator Invalidation
Modular Exponentiation for High Numbers in C++
Pthreads: Thread Starvation Caused by Quick Re-Locking
Is There an Iterator Across Unique Keys in a Std::Multimap