I have a class which I create an array/vector of, so I require a default constructor. However, this class has a data member which is a reference. The reference needs to be initialized in the constructor and I cannot do this in a default constructor because I cannot.
How should I solve this?
class MyClass{
public:
MyClass(); //Cannot instantiate s??
MyClass(Something& s);
Something& s;
}
MyClass array[10]; //MyClass needs a default constructor but a default
//constructor wont be able to initialize s
A class with a reference member needs to set the reference in its constructors. In most cases this means, that the class cannot have a default constructor. The best way to solve the problem is use a pointer instead of a reference:
class MyClass{
public:
MyClass() : s_(0) {}
MyClass(Something* s) : s_(s) {}
Something* s_;
}
As I commented above, by the description alone, I would say that it's a classical case where s
should be a Something*
rather than a Something&
...
OTOH, this work perfectly, so you don't need a default constructor if you just initialize each element of your array:
struct Something { };
struct MyClass {
MyClass(Something& ss) : s{ss} {}
Something& s;
};
int main() {
Something a, b, c, d;
Something v[10] = { a, b, c, d, a, b, c, d, a, b };
return 0;
}
You got two options here. First - and better - change Something& s;
to Something* s;
and initialize it inside the contructor like this:
MyClass(){this->s = 0;};
MyClass(Something& s){this->s = *s;};
And second - worse - create a special container for Something
:
struct somethingContainer{
Something &s;
somethingContainer(Something &s): s(s){};
};
class MyClass{
somethingContainer *container;
};
In that case - you have to allocate the memory manually with new
, and clean it with delete
.
I have a class which I create an array/vector of, so I require a default constructor. However, this class has a data member which is a reference. The reference needs to be initialized in the constructor and I cannot do this in a default constructor because I cannot.
How should I solve this?
class MyClass{
public:
MyClass(); //Cannot instantiate s??
MyClass(Something& s);
Something& s;
}
MyClass array[10]; //MyClass needs a default constructor but a default
//constructor wont be able to initialize s
A class with a reference member needs to set the reference in its constructors. In most cases this means, that the class cannot have a default constructor. The best way to solve the problem is use a pointer instead of a reference:
class MyClass{
public:
MyClass() : s_(0) {}
MyClass(Something* s) : s_(s) {}
Something* s_;
}
As I commented above, by the description alone, I would say that it's a classical case where s
should be a Something*
rather than a Something&
...
OTOH, this work perfectly, so you don't need a default constructor if you just initialize each element of your array:
struct Something { };
struct MyClass {
MyClass(Something& ss) : s{ss} {}
Something& s;
};
int main() {
Something a, b, c, d;
Something v[10] = { a, b, c, d, a, b, c, d, a, b };
return 0;
}
You got two options here. First - and better - change Something& s;
to Something* s;
and initialize it inside the contructor like this:
MyClass(){this->s = 0;};
MyClass(Something& s){this->s = *s;};
And second - worse - create a special container for Something
:
struct somethingContainer{
Something &s;
somethingContainer(Something &s): s(s){};
};
class MyClass{
somethingContainer *container;
};
In that case - you have to allocate the memory manually with new
, and clean it with delete
.
0 commentaires:
Enregistrer un commentaire