#ifndef FIXEDHPP #define FIXEDHPP include <iostream> include <string> include <iomanip>…
- User
#ifndef FIXED_HPP #define FIXED_HPP
include
include
include
class Fixed { private: int Integer; static const int IntegerStatic = 8; public: //constructeur par defaut Fixed() : valeur(0) { std::cout << "constructeur par defaut est appele" << std::endl; } //constructeur de copie Fixed(const Fixed &autre) : Integer(autre.Integer) { std::cout << "Le constructeur de recopie est appele" << std::endl; } //surcharge Fixed& operator=(const Fixed& autre) { if (this != &autre) { Integer = autre.Integer; } std::cout << "operateur d'affectation est appele" << std::endl; return *this; } //destructeur ~Fixed() { std::cout << "le destructeur est apple" << std::endl; } } ques que return *this; retourn?
- Assistant
The line
return *this;in your overloaded assignment operator function (operator=) is used to return the current object (that is, the object to which the assignment operator was applied, or the left-hand side of the=operator).When you write this code:
Fixed a; Fixed b; a = b;- The
operator=function is invoked withaasthisandbas theautreargument. - The function performs the assignment (
Integer = autre.Integer). - At the end of the function,
return *this;returns the current objecta(the left-hand side of the assignment) as the result of the assignment operation.
So, effectively, provides the ability to chain assignments. For example:
- The
- User
#ifndef FIXED_HPP #define FIXED_HPP
include
include
include
class Fixed { private: int Integer; static const int IntegerStatic = 8; public: //constructeur par defaut Fixed() : valeur(0) { std::cout << "constructeur par defaut est appele" << std::endl; } //constructeur de copie Fixed(const Fixed &autre) : Integer(autre.Integer) { std::cout << "Le constructeur de recopie est appele" << std::endl; } //surcharge Fixed& operator=(const Fixed& autre) { if (this != &autre) { Integer = autre.Integer; } std::cout << "operateur d'affectation est appele" << std::endl; return *this; } //destructeur
Fixed() { std::cout << "le destructeur est apple" << std::endl; } int getRawBits( void ) const; void setRawBits( int const raw ); } ┌──(overlord㉿Overlord)-[/Bureau/cpp02/ex00] └─$ make c++ -Wall -Werror -Wextra -std=c++98 -c main.cpp -o obj/main.o In file included from main.cpp:2: Fixed.hpp:1: error: unterminated #ifndef 1 | #ifndef FIXED_HPP Fixed.hpp:42:8: error: expected ‘;’ after class definition 42 | } | ^ | ; Fixed.hpp: In constructor ‘Fixed::Fixed()’: Fixed.hpp:16:27: error: class ‘Fixed’ does not have any field named ‘valeur’ 16 | Fixed() : valeur(0) | ^~~~~~ make: *** [Makefile:24 : obj/main.o] Erreur 1 - Assistant