Const reference passing in constructor

Discussion in 'C++' started by Rahul Karnik, Jun 21, 2011.

  1. Rahul Karnik

    Rahul Karnik Guest

    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
    Rahul Karnik, Jun 21, 2011
    #1
    1. Advertising

  2. 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.
    Branimir Maksimovic, Jun 21, 2011
    #2
    1. Advertising

  3. Rahul Karnik

    Paul Guest

    "Rahul Karnik" <> wrote in message
    news:...
    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.
    Paul, Jun 21, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,102
  2. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,228
  4. George2
    Replies:
    10
    Views:
    595
    Pete Becker
    Dec 17, 2007
  5. 0m
    Replies:
    26
    Views:
    1,115
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page