Struct constructor + function parameter
I am a C ++ beginner. I have the following code, the result is not what I expect. The question is why, acc. what's wrong. Surely most of you see this at first glance.
struct Complex {
float imag;
float real;
Complex( float i, float r) {
imag = i;
real = r;
}
Complex( float r) {
Complex(0, r);
}
std::string str() {
std::ostringstream s;
s << "imag: " << imag << " | real: " << real << std::endl;
return s.str();
}
};
class Complexes {
std::vector<Complex> * _complexes;
public:
Complexes(){
_complexes = new std::vector<Complex>;
}
void Add( Complex elem ) {
_complexes->push_back( elem );
}
std::string str( int index ) {
std::ostringstream oss;
Complex c = _complexes->at(index);
oss << c.str();
return oss.str();
}
};
int main(){
Complexes * cs = new Complexes();
//cs->Add(123.4f);
cs->Add(Complex(123.4f));
std::cout << cs->str(0); return 0; }
at the moment I'm interested in C ++ basics, not in complexnumber theory ;-) It would be nice if the Add function also took one real (no additional overloading) and not just a Complex-object, is that possible?
thanks a lot in advance Sorry,
a source to share
You cannot call one constructor on the body of another of this type:
Complex( float r) {
Complex(0, r);
}
In C ++, a temporary object of the Complex class is created, which is immediately destroyed.
You can use default parameters in constructor or some private method that will be called by constructors
a source to share
Complex( float i, float r) {
imag = i;
real = r;
}
Complex( float r) {
Complex(0, r);
}
This pair of constructors probably doesn't do what you intend.
The body of the second constructor creates an unnamed temporary complex object, which is then discarded. Members real
and are imag
not initialized.
The simplest solution is as follows:
Complex( float r )
: imag(0), real(r)
{
}
C ++ does not currently support delegating constructors, so you cannot call one constructor from another.
As Neil Butterworth points out, currently your Complexes class is probably getting complicated with dynamically allocated vector
from Complex
. You would probably be better off using a type object vector
rather than a pointer. With a dynamically allocated vector, you must provide the destrutor, copy construtor and copy statements that you are not currently executing in order to delete
use the vector and control copying it at the appropriate points. Without it, your class will be (at best) a memory leak.
a source to share
Of course, just add:
void Add( float elem ) {
_complexes->push_back( elem );
}
C ++ will call the constructor to create a complex from float. However, if many of these functions with the same name could lead to ambiguity, don't go overboard.
I would also observe that you don't need to create your Complexity vector dynamically - you should avoid dynamically creating objects in C ++ where possible. And the default choice for real data type should be double, not float.
You can also use the built-in std :: complex class for complex numbers. It comes with arithmetic operators like +, -, *, ... and other functions. This will give you a little hassle with the correct math.
a source to share