Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7004 > unrolled thread

Const reference passing in constructor

Started byRahul Karnik <rahul@genebrew.com>
First post2011-06-21 05:39 -0700
Last post2011-06-21 14:47 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  Const reference passing in constructor Rahul Karnik <rahul@genebrew.com> - 2011-06-21 05:39 -0700
    Re: Const reference passing in constructor Branimir Maksimovic <bmaxa@hotmail.com> - 2011-06-21 15:20 +0200
    Re: Const reference passing in constructor "Paul" <pchristor@yahoo.co.uk> - 2011-06-21 14:47 +0100

#7004 — Const reference passing in constructor

FromRahul Karnik <rahul@genebrew.com>
Date2011-06-21 05:39 -0700
SubjectConst reference passing in constructor
Message-ID<8cf0da8f-416c-434b-b876-293f1018b139@glegroupsg2000goo.googlegroups.com>
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::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.

Thanks for the help,
Rahul

[toc] | [next] | [standalone]


#7007

FromBranimir Maksimovic <bmaxa@hotmail.com>
Date2011-06-21 15:20 +0200
Message-ID<itq5re$3p4$1@solani.org>
In reply to#7004
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.

[toc] | [prev] | [next] | [standalone]


#7009

From"Paul" <pchristor@yahoo.co.uk>
Date2011-06-21 14:47 +0100
Message-ID<Ah1Mp.6986$wK7.4059@newsfe13.ams2>
In reply to#7004
"Rahul Karnik" <rahul@genebrew.com> wrote in message 
news:8cf0da8f-416c-434b-b876-293f1018b139@glegroupsg2000goo.googlegroups.com...
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::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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web