Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

member variable reference

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

792 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