Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference member variables as class members

Tags:

c++

reference

In my place of work I see this style used extensively:-

#include <iostream>  using namespace std;  class A { public:    A(int& thing) : m_thing(thing) {}    void printit() { cout << m_thing << endl; }  protected:    const int& m_thing; //usually would be more complex object };   int main(int argc, char* argv[]) {    int myint = 5;    A myA(myint);    myA.printit();    return 0; } 

Is there a name to describe this idiom? I am assuming it is to prevent the possibly large overhead of copying a big complex object?

Is this generally good practice? Are there any pitfalls to this approach?

like image 918
Angus Comber Avatar asked Sep 12 '12 11:09

Angus Comber


People also ask

How do you reference class members within a class?

The members of a class are referenced (accessed) by using the object of the class followed by the dot (membership) operator and the name of the member. The members of a class are referenced (accessed) by using the object of the class followed by the dot (membership) operator and the name of the member.

How do you access member variables in a class?

To access class variables, you use the same dot notation as with instance variables. To retrieve or change the value of the class variable, you can use either the instance or the name of the class on the left side of the dot.

Can classes have member variables?

There are several kinds of variables: Member variables in a class—these are called fields. Variables in a method or block of code—these are called local variables. Variables in method declarations—these are called parameters.

Can a class member be a reference C++?

There are a few important points to note when using references as class members: You need to ensure that the referred object is guaranteed to exist till your class object exists. You need to initialize the member in the constructor member initializer list.


1 Answers

Is there a name to describe this idiom?

In UML it is called aggregation. It differs from composition in that the member object is not owned by the referring class. In C++ you can implement aggregation in two different ways, through references or pointers.

I am assuming it is to prevent the possibly large overhead of copying a big complex object?

No, that would be a really bad reason to use this. The main reason for aggregation is that the contained object is not owned by the containing object and thus their lifetimes are not bound. In particular the referenced object lifetime must outlive the referring one. It might have been created much earlier and might live beyond the end of the lifetime of the container. Besides that, the state of the referenced object is not controlled by the class, but can change externally. If the reference is not const, then the class can change the state of an object that lives outside of it.

Is this generally good practice? Are there any pitfalls to this approach?

It is a design tool. In some cases it will be a good idea, in some it won't. The most common pitfall is that the lifetime of the object holding the reference must never exceed the lifetime of the referenced object. If the enclosing object uses the reference after the referenced object was destroyed, you will have undefined behavior. In general it is better to prefer composition to aggregation, but if you need it, it is as good a tool as any other.

like image 52
David Rodríguez - dribeas Avatar answered Sep 29 '22 21:09

David Rodríguez - dribeas