Solved

member variable reference

Posted on 2013-12-12
5
370 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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

919 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

21 Experts available now in Live!

Get 1:1 Help Now