member variable of type reference of class having no default constructor

This is a discussion on member variable of type reference of class having no default constructor within the C++ Programming forums, part of the General Programming Boards category; How can I use reference of a Class type which doesnt have a no argument default constructor as a member ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    member variable of type reference of class having no default constructor

    How can I use reference of a Class type which doesnt have a no argument default constructor as a member variable in another class ?
    I am using references here like this.
    x.h
    Code:
    class X{
      public:
        X(int v);
        ~X();
      private:
        int val;
    };
    x.cpp
    Code:
    #include "x.h"
    
    X::X(int v): val(v){}
    X::~X(){}
    y.h
    Code:
    #include "x.h"
    
    class X;
    
    class Y{
      public:
        Y();
        ~Y();
      private:
        X& xi;
    };
    y.cpp
    Code:
    #include "y.h"
    
    Y::Y():xi(2){}
    Y::~Y(){}
    The above yields compilation errors
    Code:
    error: invalid initialization of non-const reference of type ‘X&’ from a temporary of type ‘int’
    If I use
    Code:
    Y::Y():xi(X(2)){
    
    }
    Compiler Fires
    Code:
    error: invalid initialization of non-const reference of type ‘X&’ from a temporary of type ‘X’
    Last edited by noobcpp; 07-29-2008 at 01:10 AM.

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    However although this works but I wonder and not sure too how much logical and leagal and structured this code is (my intention is safe null reference somehow).

    Code:
    Y::Y():xi(*(new X(2))){
    
    }
    the above compiles;

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    How can I use reference of a Class type which doesnt have a no argument default constructor as a member variable in another class ?
    By storing an X reference member, there is the implication that this member is not owned by the Y object. Consequently, I would expect the Y constructor to take an X&, and then use this argument to initialise the reference member. You probably want to either implement the copy constructor in a similiar way, or disable it by declaring it as a private member. You should also probably just declare as private the copy assignment operator.

    However although this works but I wonder and not sure too how much logical and leagal and structured this code is (my intention is safe null reference somehow).
    It's legal, but then your Y object now pretty much has ownership over the X object in question, so you should simply store an object or a (smart) pointer instead of a reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    actually its kinda Circular Composition so the constructor just cant take the reference.
    I am trying to explaining more.
    Code:
    class Request: public RawRequest{
      private:
        Session& session;//Request object have a Session reference which must be initialized through initializer list
      public:
        Request(HttpHeader& headerRefObj);
        ~Request();
      .....
    };
    Code:
    class Session{
      public:
        Session(Request& req);//Session must have a Request Object
        Session(Request& req, const string&);
        ~Session();
      private:
        Request& requestRef;
      .....
    };
    so the Dilemma is Session class cant be instantiated before Request but the request Class must initialize the the Session& member variable through its constructor.

    so what I am doing is:
    Code:
    HttpHeader header;
    Request request(header);
    //Here the Request's constructor will use a way similer to *(new X(2)) show before
    Session session(request);
    but the problem with it is *(new X(2)) will stay in memory untill and unless the delete is called. and as its an anonumoys variable I cant delete it from the destructor.

    so there will be a big memory leack cause both Request and session require more or less huge ammount of memory and whenever the application is used a chunk of memory is wasted.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    What is the relationship between Session and Request? From your code, it looks like a Request owns its Session, but the Session maintains a reference to the Request that owns it. If this is the case, then store a (smart) pointer to the Session in Request, upon which you can do something like:
    Code:
    Request::Request(HttpHeader& headerRefObj)
    {
        session = new Session(*this);
        // ...
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    No both of Session and Request is owned by itself but both of these have a reference to another one. e.g. Session has a reference to request and request has the reference to session.
    and there will be only one instance of session and one instance of request.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by noobcpp View Post
    No both of Session and Request is owned by itself but both of these have a reference to another one. e.g. Session has a reference to request and request has the reference to session.
    and there will be only one instance of session and one instance of request.
    Why do you want each object to have a reference to the other, but yet, neither owns the other - usually there is a owner/owned relationship when you have references to other objects - that way, it's clear who creates which object, and when/where it gets destroyed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21