Passing a reference to an internal class

From: Peter Gordon (petergo_at__deleteme_.netspace.net.au)
Date: 03/14/05


Date: Mon, 14 Mar 2005 00:46:37 +0000 (UTC)

Is it possible to pass a reference defined in a class
to an internal class? In the code below, the constructor
for class B does not compile.

#include <fstream>

class A {
public:
    A(std::ifstream& _afin) : afin(_afin) {}
    std::ifstream& GetFileH() { return afin; }

    class B {
        B(std::ifstream& _bfin) :
                bfin( _bfin = A::GetFileH(); ) {} // B constructor
    private:
        std::ifstream& bfin;
    };

private:
    std::ifstream& afin;
};

int main() {
    std::ifstream fileH;
    fileH.open("afile");
    class A x(fileH);
    return 0;
}



Relevant Pages

  • Re: why does it crash?
    ... >> Lack of a copy constructor in class ds. ... Declare a private copy constructor and an assignement operator in ... and try to compile again. ... or an assignment operator is needed, ...
    (comp.lang.cpp)
  • Re: class constants
    ... > constructor Create; ... > I fully did not expect this to compile, ... Delphi 8 has class ...
    (borland.public.delphi.non-technical)
  • Re: private constructors
    ... having trouble figuring out why this doesn't compile: ... > It says that B does not have access to A's default constructor, ... This doesn't compile either ...
    (comp.lang.cpp)
  • Re: Default constructors, passing argument
    ... Because the code I wrote won't compile if it private. ... create an instance of MySubClassObject. ... It can't do it if the constructor ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Private instance variables available everywhere within constructed object
    ...   var private; ...   function Constructor() ... instance's private variables, we can ensure that each instance has its ...
    (comp.lang.javascript)