Sign in to follow this  
imnsain

help understanding

Recommended Posts

Hello there. I am quite a beginer but have a good understanding of the basics of c++ programing. I have been reading "BEGINNING C++ GAME PRGRAMMING" by Michael Dawson. So far the book has been a very good read. But i have come across a concept that i eather am missing or just missed it in the book. Please help me understand. I will copy the program from the book then ask my question.
//Critter Farm
//Demonstrates object containment

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Critter
{
public:

    Critter(const string& name = ""): m_Name(name) {}
    string GetName() const { return m_Name; }  
private:
    string m_Name;
};

class Farm
{
public:
    Farm(int spaces = 1) { m_Critters.reserve(spaces); }
    void Add(const Critter& aCritter) { m_Critters.push_back(aCritter); }
    void RollCall() const
    {
        for (vector<Critter>::const_iterator iter = m_Critters.begin(); 
             iter != m_Critters.end(); ++iter)
            cout << iter->GetName() << " here.\n";
    }
private:
    vector<Critter> m_Critters;
};

int main()
{
    Critter crit("Poochie");
    cout << "My critter's name is " << crit.GetName() << endl; 

    cout << "\nCreating critter farm.\n";
    Farm myFarm(3);
    
    cout << "\nAdding three critters to the farm.\n";
    myFarm.Add(Critter("Moe"));
    myFarm.Add(Critter("Larry"));
    myFarm.Add(Critter("Curly"));

    cout << "\nCalling Roll...\n";
    myFarm.RollCall();
   
    return 0;
}

My question is ...I thought that class critter could only make a object as in "Critter crit("Poochie");" but i am a bit confused when the "myFarm.Add(Critter("Moe"));" tryes to make "Moe" a string literal into a object name in " void Add(const Critter& aCritter) { m_Critters.push_back(aCritter); }". I guess i just dont understand what all the Class Critter is able to do other than make objects. Please help me find where in the book it explanes this or please help me with a explination of your own. Thanks alot //EDIT: I put your code into source tags. You can learn how in the FAQ. - Kaz [Edited by - Kazgoroth on August 31, 2005 3:20:47 AM]

Share this post


Link to post
Share on other sites
The constructor for Critter is defined as:
Critter(const string& name = ""): m_Name(name) {}

All Critter( "Moe" ) does is return an object with the value of m_Name set to "Moe".

So in this case, myFarm.Add(Critter("Moe")); is firstly creating the object of class Critter with an m_Name of Moe and then returning that object as the parameter for the Add() method of the object myFarm.

Share this post


Link to post
Share on other sites
Thanks alot for your help. Was trying to wrap my brain around it and that is what i figured but nowhere i could find would just say what you just said heh.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this