answersLogoWhite

0


Best Answer

References must be initialised from the initialisation list of all the class constructors. This is not unusual, because all instance member variables, particularly embedded members, whether references or not, should always be initialised from the initialisation list in order to ensure the most efficient construction. The initialisation list also allows you to invoke specific base class constructors (where required), rather than the default base class constructor that would be called implicitly.

Note that you cannot initialise a reference with a default value (not even NULL), thus you cannot declare any constructor that does not implicitly pass the required references. In particular, this means you must not declare a default constructor (the compiler-generated version is not generated so long as you declare at least one other constructor). You must also explicitly disable the compiler-generated assignment operator because references cannot be re-assigned once instantiated. You disable this method by declaring it private, but without providing an implementation.

The following stripped-down example shows how to initialise a class that contains two member references:

class foo1 { /*...*/ };

class foo2 { /*...*/ }; // definitions omitted for brevity

class bar {

private:

// member references:

foo1& m_foo1;

foo2& m_foo2;

public:

// ctor and copy ctor with initialisation lists:

bar(foo1& f1, foo2& f2):m_foo1(f1), m_foo2(f2) {}

bar(const bar& b): m_foo1(bar.m_foo1), m_foo2(bar.m_foo2) {}

private:

// assignment operator (disabled):

bar& operator= (const bar&);

};

Note that the assignment operator is declared but is not implemented. Do not include curly braces in the declaration otherwise the method is not disabled and the compiler will complain about the lack of a return value in the implementation.

Note also that definitions can also be split from declarations as required,by defining the initialisation list in the source file rather than the header, like so:

// file: bar.hpp

class bar {

private:

// member references:

foo1& m_foo1;

foo2& m_foo2;

public:

// ctor and copy ctor:

bar(foo1& f1, foo2& f2);

bar(const bar& b);

private:

// assignment operator disabled:

bar& operator= (const bar&);

};

// file: bar.cpp

#include "bar.hpp"

bar::bar(foo1& f1, foo2& f2):m_foo1(f1), m_foo2(f2) {}

bar::bar(const bar& b): m_foo1(bar.m_foo1), m_foo2(bar.m_foo2) {}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Where does one initialize a Reference member of a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the purpose of constructor in object oriented programming?

A constructor in a class is one of the first pieces of code to be executed when you instantiate a class. The purpose of constructors is to have code that initialize the class and prepare the variables that may be required by the class...


What are the different ways in which an object can access another object in a language like C plus plus?

Accessibility to class members is determined by whether those members are declared private, protected or public. Private members of a class are accessible to all instances of that same class, as well as friends of the class. Protected members of a class are accessible to all instances of that same class, as well as friends of the class, and also classes derived from the class. Public members are accessible, both inside and outside of the class, including other classes. How you access the members of a class depends on whether you're dealing with an object reference or a pointer to an object and whether the methods are virtual or not. Accessing members of an object reference is via the reference member operator (.), while members of a pointer to an object are accessed via the indirection operator (->). If a member method is virtual, the most-derived member method will be invoked, regardless of whether a reference or pointer refers to a derived class or one of its base classes. To specifically call a base class virtual method, you must use the scope resolution operator (::) to explicitly specify which class method you actually want to call.


How does one initialize a byte array in Java?

One can get information about how to initialize a byte array in java on the website stackoverflow dot com. That website can learn one a lot about java.


What is the difference between static data member and ordinary data member?

Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.


How might one become a member of middle class?

by loving


What is static variable in the class for c plus plus?

Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.


What meaning by the terms 'member function'and give example?

In object-oriented programming languages, a member function is one which belongs to a class.


When a class is created with two integer members and one member function what is the class size?

Platform-dependent, that's what sizeof is good for.


Why keyword static is to include in main method?

A class may have both static and non-static methods. Non-static methods are local to objects of the class (instances of the class) and are often known as instance methods for that reason. Being local to an object means they can refer to the current instance of the class through the implicit "this" reference. Static methods, on the other hand, are local to the class and do not have an implicit "this" reference. As such, static methods can be invoked even when no instances of the class are in scope. Since the main function is the entry point of the application and there can be only one entry point, it doesn't make any sense to declare it an instance member because no objects of the class would physically exist.


What is the use of 'this' pointer?

Every instance of a class inherits a 'this' pointer. It always points to the instance itself. Outside of the object you must use the object's variable name to refer to the object, or instantiate a pointer to the object. But from within the object's member methods you must use the 'this' pointer which is instantiated automatically as soon as the object is constructed and falls from scope when the destructor returns. Only non-static member functions have access to the 'this' pointer. There are several uses, however the most important is when checking for self-references, particularly in the assignment operator overload. That is, any member function that accepts a reference to the same class of object should always check for self-references before attempting to mutate the instance. This is particularly important when the class "owns" memory that is dynamically allocated to it. It is also used to return a reference to the current instance from the assignment operator and from any other operator overload or function that must return a reference to the current instance (including the addition and subtraction operators). Both uses can be seen in the following stripped-down example: class MyObject { public: // Assignment operator overload.MyObject& operator= ( const MyObject & obj ){ // Self-reference check. if( this != &obj ){// Assignment code goes here... } // Return a reference to this object. return( *this ); } }; The 'this' pointer can also be used to pass a pointer (this) or reference (*this) to external functions that accept such arguments. It should also be noted that when referring to an instance member from within a non-static member function, the dereferenced 'this' pointer is implied, as in: this->[member_name] Although this usage is never required, there may be times when it can help make your code more readable, or less ambiguous, especially when a member function must handle one or more external instances of the same class.


Difference between member function and friend function?

1. Function - is normally refered to a C-style function which has a global level scope. As long as its declaration is visible in a file where it is being used, and the definition is available somewhere in the application, the linker will find the definition and link to it. It can be used anywhere in the application. 2. Member function - is normally referred to a C++Style method declared/defined inside of a C++ class. The scope for such member functions is the class. They are not accessible outside the class and are only accessible thru an object/instance of such a class. There are, of course, exceptions to this, such as static and friends.


What is the purpose of 'this' operator in c?

Every instance of a class inherits a 'this' pointer. It always points to the instance itself. Outside of the object you must use the object's variable name to refer to the object, or instantiate a pointer to the object. But from within the object's member methods you must use the 'this' pointer which is instantiated automatically as soon as the object is constructed and falls from scope when the destructor returns. Only non-static member functions have access to the 'this' pointer. There are several uses, however the most important is when checking for self-references, particularly in the assignment operator overload. That is, any member function that accepts a reference to the same class of object should always check for self-references before attempting to mutate the instance. This is particularly important when the class "owns" memory that is dynamically allocated to it. It is also used to return a reference to the current instance from the assignment operator and from any other operator overload or function that must return a reference to the current instance (including the addition and subtraction operators). Both uses can be seen in the following stripped-down example: class MyObject { public: // Assignment operator overload.MyObject& operator= ( const MyObject & obj ){ // Self-reference check. if( this != &obj ){// Assignment code goes here... } // Return a reference to this object. return( *this ); } }; The 'this' pointer can also be used to pass a pointer (this) or reference (*this) to external functions that accept such arguments. It should also be noted that when referring to an instance member from within a non-static member function, the dereferenced 'this' pointer is implied, as in: this->[member_name] Although this usage is never required, there may be times when it can help make your code more readable, or less ambiguous, especially when a member function must handle one or more external instances of the same class.