Solved

member variable reference

Posted on 2013-12-12
5
324 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 24

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 32

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 28

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 110
Fully specialized class template function 21 118
Best book to learn C++ 4 53
Safe conversion? 4 17
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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now