Why no compile time errors?

The following did not create a compile time error, instead it returned a default object and I don't know why.

in complex0.cpp
1
2
3
4
5
6
7
complex complex::operator~() const
{
	complex temp;
	temp.real = real;
	temp.imaginary = -imaginary;
	return temp;	//when this is omitted no compile error?
}


in complex0.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class complex
{
private:
	double real;
	double imaginary;
public:
	complex(double a = 0.0, double b = 0.0);	//a = real, b = imaginary
	complex operator+(const complex& num) const;
	complex operator-(const complex& num) const;
	complex operator*(const complex& num) const;
	complex friend operator*(double num, const complex& num2);
	complex operator~() const; //RELEVANT PROTOTYPE FOR QUESTION
	friend std::ostream& operator<<(std::ostream& os, const complex& num);
	friend std::istream& operator>>(std::istream& is, complex& num);	
};


in main.cpp I used the operator and it returned a default object when the return statement was omitted, I don't know why the default constructor was used then returned instead of the compiler yelling at me for not returning anything?

Also another run-time instead of compile-time error occurred when I was writing the definition for overloading the >> operator. The compiler didn't complain when I had the following -
prototype:
friend std::istream& operator>>(std::istream& is, const complex& num); //note const
definition:
1
2
3
4
5
6
7
8
9
10
std::istream& operator>>(std::istream& input_stream, const complex& num)
{
	std::cout << "real: ";
	if (input_stream >> num.real) //RUN-TIME ERROR HERE WITH CONST
	{
		std::cout << "imaginary: ";
		input_stream >> num.imaginary;	//newline left in input
	}
	return input_stream;
}

Had an infinite loop where "real: " was printed over and over until the program crashed, but again it was a run-time error and not a compile-time error. Would've expected the compiler to complain about me trying to modify a constant object instead of allowing the program to compile.
Can't really answer your first question, but as for your second question:

http://www.parashift.com/c++-faq/ref-to-const.html
You get a compile-time error if you call any functions or perform any operations that mutate the object state in some way. In your program, you're just recursively calling the same function over and over again, which does not actually change any of your object members.
The first one is not a syntactic error. Compile with warnings (and elevate warnings to errors)
-W{all,extra,pedantic,error}

> it returned a default object when the return statement was omitted
undefined behaviour

> Would've expected the compiler to complain about me trying to modify a constant object
make your constructor explicit
The things is that you never modify the object. When you try to do input_stream >> num.real `num.real' is interpreted as a complex number, as if you have called input_stream >> complex(num.real)
Last edited on
Thank you for the replies, still have a question though.
In your program, you're just recursively calling the same function over and over again


When you try to do input_stream >> num.real `num.real' is interpreted as a complex number, as if you have called input_stream >> complex(num.real)


When these are put together it makes sense to me (after thinking about it for awhile) since I'm trying to define the overloaded >> in terms of itself if num.real is interpreted as a complex object. But then why does then code work properly when const is removed? Why is "num.real" no longer interpreted as "complex(num.real)"?
If you have const complex &num then `num.real' can't be modified, it is a const double
There is no std::istream& operator>>(std::istream& input_stream, const double num) overload
So the best guess of the compiler is to convert the `const double' to a `const complex &' (for which an overload exists)

If you have complex &num then `num.real' is simply a double and the std::istream& operator>>(std::istream& input_stream, double &num) overload is used
Last edited on
If you have complex &num then `num.real' is simply a double and the std::istream& operator>>(std::istream& input_stream, double &num) overload is used

I just had a "oh derp" moment when I read this, seems obvious now. Thanks.
Topic archived. No new replies allowed.