Results 1 to 4 of 4

Thread: Class question - solved

Hybrid View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166

    Class question - solved

    Why do I have to have these things that are marked in bold below in the code?

    Code:
    class keeper
    {
    private:
    	int hours;
    	int minutes;
    public:
    	keeper();
    	keeper(int s, int p);
    	void addKeeper(int, int);
    	void writeKeeper(keeper &k);
    	keeper getKeeper(keeper &h, char *name);	keeper operator+(const keeper &k) const;
    	void show();
    	~keeper() {};
    };
    .
    .
    .
    keeper keeper::getKeeper(keeper &h, char *name)
    {
    	ifstream fin;
    	fin.open(name);
    
    	for(int i=0;i<3;i++)
    	{
    		switch(i)
    		{
    		case 0: fin >> h.hours;
    			break;
    		case 1: fin >> h.minutes;
    			break;
    		}
    	}
    	fin.close();
    
    	return h;
    
    
    }
    I know I can't have void keeper::........, but why do I have to have keeper beofre the code?

    thanx
    Last edited by [praetorian]; Dec 28th, 2001 at 05:29 PM.
    [p r a e t o r i a n]

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You're returning a copy of a reference already passed into the function

    Either take out the reference being passed, or change it to a void. When you pass a reference you can change it from the function.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I think you should have keeper& as return value

    It's quite useful when building expresions with the sideeffect-paradigm

    For instance:

    char source[10]="something";
    char* copy=strncpy(new char[10],source,10);
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    I'm mobile
    Posts
    166
    Problem solved thanks to Kedaman and Parksie, thank you!
    [p r a e t o r i a n]

Posting Permissions

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



Click Here to Expand Forum to Full Width