如何实现未命名的参数

时间:2015-05-15 14:16:25

标签: c++ parameter-passing

我有一个问题。

'use strict';

function ChatAppCtrl($scope, $q, $modal, socket, useragent, geolocation) {
  $scope.peopleCount = 0;
  $scope.messages = [];
  $scope.user = {}; //holds information about the current user
  $scope.users = {}; //holds information about ALL users
  $scope.rooms = []; //holds information about all rooms
  $scope.error = {};
  $scope.typingPeople = [];
  $scope.username = '';
  $scope.joined = false;
  var typing = false;
  var timeout  = undefined;

  /* ABOUT PAGE */
  $scope.about = function() {
    var modalInstance = $modal.open({
      templateUrl: 'aboutModal',
      controller: aboutModalCtrl
    });
  };

  var aboutModalCtrl = function($scope, $modalInstance) {
    $scope.cancel = function() {
      $modalInstance.dismiss('cancel');
    };
  };
  /* ABOUT PAGE END */

  $scope.setUsername = function(suggestedUsername) {
    $scope.username = suggestedUsername;
  }

  function timeoutFunction() {
    typing = false;
    socket.emit('typing', false);
  }

  $scope.focus = function(bool) {
    $scope.focussed = bool;
  }
  $scope.typing = function(event, room) {
    if (event.which !== 13) {
      if (typing === false && $scope.focussed && room !== null) {
        typing = true;
        socket.emit('typing', true);
      } else {
        clearTimeout(timeout);
        timeout = setTimeout(timeoutFunction, 1000);
      }
    }
  }

  socket.on('isTyping', function(data) {
    if (data.isTyping) {
      $scope.isTyping = data.isTyping;
      $scope.typingPeople.push(data.person);
    } else {
      $scope.isTyping = data.isTyping;
      var index = $scope.typingPeople.indexOf(data.person);
      $scope.typingPeople.splice(index, 1);
      $scope.typingMessage = '';
    }
  });

  $scope.joinServer = function() {
    $scope.user.name = this.username;
    if ($scope.user.name.length === 0) {
      $scope.error.join ='Please enter a username';
    } else {
      var usernameExists = false;
      socket.emit('checkUniqueUsername', $scope.user.name, function(data) {
        usernameExists = data.result;
        if (usernameExists) {
          $scope.error.join = 'Username ' + $scope.user.name + ' already exists.';
          socket.emit('suggest', $scope.user.name, function(data) {
            $scope.suggestedUsername = data.suggestedUsername;
          });
        } else {
          socket.emit('joinSocketServer', {name: $scope.user.name});
          $scope.joined = true;
          $scope.error.join = '';
        }
      });
    }
  }

  $scope.send = function() {
    if (typeof this.message === 'undefined' || (typeof this.message === 'string' && this.message.length === 0)) {
      $scope.error.send = 'Please enter a message';
    } else {
      socket.emit('send', {
        name: this.username,
        message: this.message
      });
      $scope.message = '';
      $scope.error.send = '';
    }

  }

  $scope.createRoom = function() {
    var roomExists = false;
    var room = this.roomname;
    if (typeof room === 'undefined' || (typeof room === 'string' && room.length === 0)) {
      $scope.error.create = 'Please enter a room name';
    } else {
      socket.emit('checkUniqueRoomName', room, function(data) {
        roomExists = data.result;
        if (roomExists) {
          $scope.error.create = 'Room ' + room + ' already exists.';
        } else {
          socket.emit('createRoom', room);
          $scope.error.create = '';
          if (!$scope.user.inroom) {
            $scope.messages = [];
            $scope.roomname = '';
          }
        }
      });
    }
  }

  $scope.joinRoom = function(room) {
    $scope.messages = [];
    $scope.error.create = '';
    $scope.message = '';
    socket.emit('joinRoom', room.id);
  }

  $scope.leaveRoom = function(room) {
    $scope.message = '';
    socket.emit('leaveRoom', room.id);
  }

  $scope.deleteRoom = function(room) {
    $scope.message = '';
    socket.emit('deleteRoom', room.id)
  }

  socket.on('sendUserDetail', function(data) {
    $scope.user = data;
  });

  socket.on('listAvailableChatRooms', function(data) {
    $scope.rooms.length = 0;
    angular.forEach(data, function(room, key) {
      $scope.rooms.push({name: room.name, id: room.id});
    });
  });

  socket.on('sendChatMessage', function(message) {
    $scope.messages.push(message);
  });

  socket.on('sendChatMessageHistory', function(data) {
    angular.forEach(data, function(messages, key) {
      $scope.messages.push(messages);
    });
  });

  socket.on('connectingToSocketServer', function(data) {
    $scope.status = data.status;
  });

  socket.on('usernameExists', function(data) {
    $scope.error.join = data.data;
  });

  socket.on('updateUserDetail', function(data) {
    $scope.users = data;
  });

  socket.on('joinedSuccessfully', function() {
    var payload = {
      countrycode: '',
      device: ''
    };
    geolocation.getLocation().then(function(position) {
      return geolocation.getCountryCode(position);
    }).then(function(countryCode) {
      payload.countrycode = countryCode;
      return useragent.getUserAgent();
    }).then(function(ua) {
      return useragent.getIcon(ua);
    }).then(function(device) {
      payload.device = device;
      socket.emit('userDetails', payload);
    });
  });

  socket.on('updatePeopleCount', function(data) {
    $scope.peopleCount = data.count;
  });

  socket.on('updateRoomsCount', function(data) {
    $scope.roomCount = data.count;
  });

  socket.on('disconnect', function(){
    $scope.status = 'offline';
    $scope.users = 0;
    $scope.peopleCount = 0;
  });
}

如何实施上述代码?我需要写void setName(const string&); ,但没有命名变量。只有this.name = ...符号。

2 个答案:

答案 0 :(得分:6)

您不需要在函数原型中包含显式参数名称。事实上,一些程序员故意省略它们。

只有在定义函数时,才需要提供参数名称,并且只有在引用到该参数时才会提供。

所以在标题中,void setName(const string&);没问题。这告诉您该函数引用string&表示引用),并且不返回任何内容。

但是当你定义这个函数时,如果你引用它,你需要提供一个参数。

答案 1 :(得分:3)

你的头文件有这个......

class MyClass
   {
   string name;
public:
   void setName(const string&);
   };

您的实施文件可以包含此内容......

void MyClass::setName(const string& s )
   {
   this->name = s;
   }