dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2018
    Posts
    50

    return value in class method

    In this code:

    Code:
    const int SIZE = 1024; // numero di byte che possono essere memorizzati in ciascun settore
    
    class Disco
    {
      unsigned int* _vett;        // vettore di settori
      int _quantiSettori;         // numero di settori nel Disco
      int _quantiLiberi;          // numero di settori attualmente liberi
      int _quantiFile;            // numero di file memorizzati fino ad ora
      static int _quantiDischi;   // numero di Dischi presenti in memoria
      
      // mascheramento costruttore di copia e dell'op. di assegnamento
      Disco(const Disco&);
      Disco& operator=(const Disco&);
    
    public:
      // --- PRIMA PARTE ---
      Disco(int);
      int riserva(int);
      friend ostream& operator<< (ostream&, const Disco&);
      Disco& cancella(int);
    
      // --- SECONDA PARTE ---  
      Disco& operator!();
      void deframmenta();
      static int getQuantiDischi(){ return Disco::_quantiDischi; };
      ~Disco(){ delete[] _vett; Disco::_quantiDischi--;};
    };


    Code:
    int Disco::_quantiDischi = 0; // inizializzazione della variabile statica 'quantiDischi'
    
    // --- PRIMA PARTE ---
    Disco::Disco(int set){
      if ( set <= 0 )
        set = 10;
      _quantiSettori = set;  
      _vett = new unsigned int[_quantiSettori];
      for (int i = 0; i < _quantiSettori; i++)
        _vett[i] = 0;
      _quantiFile = 0;
      _quantiLiberi = _quantiSettori;
      Disco::_quantiDischi++;
    }
    
    int Disco::riserva(int dim)
    {
       int quantiNecessari = (dim / SIZE); // calcolo il numero di settori necessari
      if (quantiNecessari*SIZE < dim)
        quantiNecessari++;
    
      if (quantiNecessari > _quantiLiberi) // non ci sono settori sufficienti da riservare al file
        return 0;
      
      _quantiLiberi -= quantiNecessari;
      _quantiFile++;
      for (int i = 0, k=0; i < _quantiSettori && k < quantiNecessari; i++)
        if (_vett[i] == 0){
          _vett[i] = _quantiFile;
          k++;
        }
        return _quantiFile;
    }
    
    Disco& Disco::cancella(int id)
    {
      for (int i = 0; i < _quantiSettori; i++)
      if (_vett[i] == id){
        _vett[i] = 0;
        _quantiLiberi++;
      }
      return *this;
    }
    
    ostream& operator<<(ostream& os, const Disco&d){
      for (int i = 0; i < d._quantiSettori; i++)
        os << d._vett[i];
      os << endl;
      return os;
    }
    
    
    // --- SECONDA PARTE ---
    void Disco::deframmenta(){ // bubble sort  
      
      for (int i = 0; i < _quantiSettori - 1; i++){
        bool flag = true;  
        for (int j = _quantiSettori - 1; j > i; j--){
          if ( _vett[j - 1] < _vett[j] ){
            flag = false;
            int aux = _vett[j];
            _vett[j] = _vett[j - 1];
            _vett[j-1] = aux;
          }      
        }    
        if (flag)      
          break;    
      }
    }
    
    Disco& Disco::operator!(){
      for (int i = 0; i < _quantiSettori; i++)
        _vett[i] = 0;  
      _quantiFile = 0;
      _quantiLiberi = _quantiSettori;
      return *this;
    }

    Can I define Disco& cancella(int) without return value?
    What is the advantage to define Disco& as return value?!

    Thanks

    void cancella(int)

    Code:
    void Disco::cancella(int id)
    {
      for (int i = 0; i < _quantiSettori; i++)
      if (_vett[i] == id){
        _vett[i] = 0;
        _quantiLiberi++;
      }
    }

    The same thing for operator!, can i define it in this way?

    Code:
    void Disco::operator!(){
      for (int i = 0; i < _quantiSettori; i++)
        _vett[i] = 0;  
      _quantiFile = 0;
      _quantiLiberi = _quantiSettori;
    }
    Last edited by zio_mangrovia; June 3rd, 2018 at 11:38 AM.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,153

    Re: return value in class method

    Quote Originally Posted by zio_mangrovia View Post
    ...
    Can I define Disco& cancella(int) without return value?
    What is the advantage to define Disco& as return value?!

    Thanks

    void cancella(int)

    Code:
    void Disco::cancella(int id)
    {
      for (int i = 0; i < _quantiSettori; i++)
      if (_vett[i] == id){
        _vett[i] = 0;
        _quantiLiberi++;
      }
    }
    Yes, you can define Disco& cancella(int) without return value.
    What is the problem, BTW?

    Quote Originally Posted by zio_mangrovia View Post
    ...
    The same thing for operator!, can i define it in this way?

    Code:
    void Disco::operator!(){
      for (int i = 0; i < _quantiSettori; i++)
        _vett[i] = 0;  
      _quantiFile = 0;
      _quantiLiberi = _quantiSettori;
    }
    Well, I never tested it . Hawever, suppose yes, you could.
    But what for?
    What is the purpose of such a method that is assumed to negate some value/expression and return the boolean value as a result?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2018
    Posts
    50

    Re: return value in class method

    Quote Originally Posted by VictorN View Post
    Yes, you can define Disco& cancella(int) without return value.
    What is the problem, BTW?

    Well, I never tested it . Hawever, suppose yes, you could.
    But what for?
    What is the purpose of such a method that is assumed to negate some value/expression and return the boolean value as a result?
    I'm learning C++ programming and I'm making exercises to test.
    I'd like to understand what is suggestion/recommendation for good programming.
    If I used both functions into expressions It could be right to return Disco& ?! But if I use them only for specific job with no expressions?!
    I'd like when it's necessary to return value or no with operator, it's easy in case of assignment but hard to understand in other cases.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,675

    Re: return value in class method

    I've moved this post out of the managed c++ forum (managed c++ is different than plain old c++).

    Whether a method returns a value depends on whether you are going to use the value when you call the method. Or if the return object makes to have.

    For example the following method wouldn't make sense to return a pointer to an object after it has been deleted:
    Code:
    Disco* Disco::DeleteObject(Disco* pDisco)
    {
      delete pDisco;
      return pDisco;
    }

  5. #5
    Join Date
    Feb 2017
    Posts
    296

    Re: return value in class method

    Quote Originally Posted by zio_mangrovia View Post
    What is the advantage to define Disco& as return value?
    The advantage of returning the object after performing an operation on the object is that it allows for chaining of operations using dot notation, like instead of this,
    Code:
    Disco disco;
    disco.cancela(1);
    disco.cancela(2);
    disco.cancela(3);
    disco.someOtherOperation();
    a user of your class can then also do this,
    Code:
    Disco disco;
    disco.cancela(1).cancela(2).cancela(3).someOtherOperation();
    So it's a design decision. Dot chaining is a very common style in the functional paradigm of programming.
    Last edited by wolle; June 4th, 2018 at 02:08 AM.

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,153

    Re: return value in class method

    [merged threads]
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.