Solved

member variable reference

Posted on 2013-12-12
5
394 Views
Last Modified: 2013-12-13
Is there any issue with this code. Can I have member variable reference in this way.
class A_1 {
 public:  
    A_1(A_2& obj) : _obj_2 ( obj) { }
    A_2& _obj_2;
};

class A_2 {
    A_1 _obj_1;
};

A_2::A_2()  : 
    _obj_1 (*this)
{ }

Open in new window

0
Comment
Question by:perlperl
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39715278
No, there's nothing wrong with this setup (given that all foward declarations are in place so it compiles). I actually favor that over the approach of linking objects by pointers.
0
 
LVL 25

Expert Comment

by:chaau
ID: 39715707
The only problem I see is with class A_2. Here is your code:
class A_2 {
    A_1 _obj_1;
};

A_2::A_2()  : 
    _obj_1 (*this)
{ }

Open in new window

In class A_2 there is a member variable A_1 _obj_1, but there is no default constructor for A_1. I believe you code will not even compile, as A_1 class cannot be instantiated using A_1 _obj_1.

I think you need create a pointer variable inside your class A_2, like this:
class A_2 {
    A_1 *_obj_1;
};

A_2::A_2()
{
 _obj_1 = new A_1(*this);
}


A_2::~A_2()
{
 delete _obj_1;
}

Open in new window

0
 
LVL 34

Expert Comment

by:sarabande
ID: 39716056
the code compiles at my system (win7 vc++ vs2012) when a forward declaration of class A_2 was added such that the reference member can be defined in class A_1, and when the default constructor of A_2 not only was defined but also was declared in class A_2.

Sara
0
 
LVL 29

Expert Comment

by:pepr
ID: 39716402
Sara is right. @chaau, there is no need for default constructor of A_1 because it is not used. The only construction of the A_1 object is done inside the A_2 instance, and it is done via the non-default constructor. I believe, the identifiers below make it more readable (A_1 changed to A, A_2 changed to B, and the member variables accordingly):
class B;    // as Sara wrote

class A {
public:  
     A(B& obj) : _b(obj) { }
     B& _b;
};

class B {
public:     // added
    B();    // as Sara wrote
    A _a;   // is initialized via the B default-constructor initializer-list
};

B::B(): 
  _a(*this)  // here the non-default constructor is used
{ 
}
                                  
                                  
int main() {
    B b;
    return 0;
}

Open in new window

Notice that public: must also be added to the class B; otherwise, the members are considered private, including the declared B() default constructor. The B instance could not be conctructed without the public.
0
 

Author Comment

by:perlperl
ID: 39716683
Please ignore my syntax and constructor :)
Usually I try to shorten the code and only focus on the key thing like I just wanted to verify if there is any issue with reference member variable

Thanks everyone
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question