Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Const reference passing in constructor

Reply
Thread Tools

Const reference passing in constructor

 
 
Rahul Karnik
Guest
Posts: n/a
 
      06-21-2011
Hi,

I get a runtime error with the following code:

#include <iostream>

using namespace std;

class A {
int n;

public:
A();
A(const int k);
int getn() const { return n; };
};

A::A() : n(0) {};

A::A(const int k) : n(k) {}

class B {
const A& myA;

public:
B(const A& anA);
int getn() const { return myA.getn(); };
};

B::B(const A& anA) : myA(anA) {}

class C {
const A& myA;
const B& myB;

public:
C(const A& anA);
int getn() const { return myB.getn(); };
};

C::C(const A& anA) : myA(anA), myB(myA) {}

class D {
A myA;
C myC;

public:
D(const int k);
int getAn() const { return myA.getn(); };
int getCn() const { return myC.getn(); };
};

D:(const int k) : myA(k), myC(myA) {}

int main() {
D myD(10);
cerr << "A: " << myD.getAn() << '\n';
cerr << "C: " << myD.getCn() << '\n';
}

As we would expect, the first line of the output is "A: 10". But the secondline causes a segmentation fault. Could someone explain why? I guess it relates to passing the reference to const A from the C constructor to the B constructor. The reason I am trying to use references is that in the real code, A is a large class and I do not want to make any copies along the way.

Thanks for the help,
Rahul
 
Reply With Quote
 
 
 
 
Branimir Maksimovic
Guest
Posts: n/a
 
      06-21-2011
On 06/21/2011 02:39 PM, Rahul Karnik wrote:
> Hi,
>
> I get a runtime error with the following code:
>
> #include<iostream>
>
> using namespace std;
>
> class B {
> const A& myA;
>
> public:


Try putting explicit constructor for B to get compile
error.

> B(const A& anA);
> int getn() const { return myA.getn(); };
> };
>
> B::B(const A& anA) : myA(anA) {}
>
> class C {
> const A& myA;
> const B& myB;
>
> public:
> C(const A& anA);
> int getn() const { return myB.getn(); };
> };
>
> C::C(const A& anA) : myA(anA), myB(myA) {}


Problem is here. B temporary is constructed from A reference
and then bound to const B reference .
Since temporary is destructed after constructing D object,
later on you have dangling reference.

 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      06-21-2011

"Rahul Karnik" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Hi,

I get a runtime error with the following code:

#include <iostream>

using namespace std;

class A {
int n;

public:
A();
A(const int k);
int getn() const { return n; };
};

A::A() : n(0) {};

A::A(const int k) : n(k) {}

class B {
const A& myA;

public:
B(const A& anA);
int getn() const { return myA.getn(); };
};

B::B(const A& anA) : myA(anA) {}

class C {
const A& myA;
const B& myB;

public:
C(const A& anA);
int getn() const { return myB.getn(); };
};

C::C(const A& anA) : myA(anA), myB(myA) {}

class D {
A myA;
C myC;

public:
D(const int k);
int getAn() const { return myA.getn(); };
int getCn() const { return myC.getn(); };
};

D:(const int k) : myA(k), myC(myA) {}

int main() {
D myD(10);
cerr << "A: " << myD.getAn() << '\n';
cerr << "C: " << myD.getCn() << '\n';
}

As we would expect, the first line of the output is "A: 10". But the second
line causes a segmentation fault. Could someone explain why? I guess it
relates to passing the reference to const A from the C constructor to the B
constructor. The reason I am trying to use references is that in the real
code, A is a large class and I do not want to make any copies along the way.

---------------------------------------------------------------------------------------

C's initialiser list does myB(myA) .
This calls a temporary constructor, MyB is not initialised properly to alias
a B object.

Remember myB is a reference and must be initialised to alias a B object. You
shouldn't be able to call a constructor on an uninitialsed reference but you
seem to have tricked the compiler into doing so.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments