Sign in to follow this  
nlbs

member variable of type class having no default constructor

Recommended Posts

say I've a Class Called X which doesn't have a default (no argument) constructor. e.g. that Class X must be instanciated with an argument e.g. X(5) so how can I make a member variable in a Class Y of type X. I dont want to allocate on Heap using X* I just want to allocate it on Stack as general. so is it Possible ??

Share this post


Link to post
Share on other sites
But I am using references here like this.
x.h
class X{
public:
X(int v);
~X();
private:
int val;
};


x.cpp
#include "x.h"

X::X(int v): val(v){}
X::~X(){}


y.h
#include "x.h"

class X;

class Y{
public:
Y();
~Y();
private:
X& xi;
};


y.cpp
#include "y.h"

Y::Y():xi(2){}
Y::~Y(){}



The above yields compilation errors
error: invalid initialization of non-const reference of type ‘X&’ 
from a temporary of type ‘int’

Share this post


Link to post
Share on other sites
Well a Reference will not create an Object. It is almost like a pointer
(well it can not be null but otherwise it is similar). Therefore it does not
create an Object of type X for you. You still have to pass a reference then.

It should work if you change the X& xi; to X xi;
or you do something like that:

in Y.h
public:
Y(X& x);
~Y();
private:
X& xi;



Y.cpp
Y::Y(X& x):xi(x){}
Y::~Y(){}


Share this post


Link to post
Share on other sites
already tried
Y::Y():xi(X(2)){

}



the above code yields compiler error
error: invalid initialization of non-const reference of type ‘X&’ 
from a temporary of type 'X'


EDIT: EDIT: EDIT: EDIT:

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

Y::Y():xi(*(new X(2))){

}



the above compiles;

Share this post


Link to post
Share on other sites
Don't use a reference. I don't know why you think you need one, but don't. Either use a [smart] pointer or store it by value.

Edit:
Quote:
Original post by nlbs

my intention is safe null reference somehow


Why? Don't do this. Really.
Edit 2:
To elaborate, think about how you would use this "null-able reference" type, especially with the code you posted allocating an X on the heap.

If you pass this type by value, then you can't delete the created X in your destructor, lest you invalidate the original reference when the local copy is destroyed. The result is either an explicit destroy or nullout function, or a memory leak. The later is terrible, the former is sub-optimal, and has no gains over a regular pointer.

If you pass this type by reference or pointer, then you gain absolutely nothing by abstracting away the original reference.

Share this post


Link to post
Share on other sites
Unless you are absolutely sure what your second code does and that is exactly what you want, you should immediately remove it. Becuase it compiles doesn't make it correct.

My question, though, is why is xi a reference at all?

Share this post


Link to post
Share on other sites
say one Object is already created from class X and I want a reference of that in another instance Y without using Pointers (I know its easiier with new I just want to do without it)

the calss will be first Instantiated with null reference.
and latter that reference will be set using some setter method.

Share this post


Link to post
Share on other sites
Quote:
Original post by nlbs
and latter that reference will be set using some setter method.


References, by nature, are const. You cannot reseat a reference. How do you propose to handle non-copyable objects with this scheme?

Really, just use a pointer. Don't create problems by trying to hack around what the language won't let you do, when there are constructs in place to do what you want.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this