Const reference passing in constructor

R

Rahul Karnik

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 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
 
B

Branimir Maksimovic

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.
 
P

Paul

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top