Can someone explain this syntax?

Can someone explain this syntax?

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
PauloMorfeo
Posts: 2004
Joined: 15 Dec 2004, 20:53

Can someone explain this syntax?

Post by PauloMorfeo » 18 Nov 2005, 21:49

1
In the file UnitHandler.cpp, we have this:

Code: Select all

CUnitHandler::CUnitHandler()
:	overrideId(-1),
	maxUnits(500),
	lastDamageWarning(0),
	lastCmdDamageWarning(0),
	metalMakerIncome(0),
	metalMakerEfficiency(1),
	diminishingMetalMakers(false),
	limitDgun(false)
{
	stuff
}
The first "::" seems to be «this is the definition of the function CUnitHandler() in the class CUnitHandler. But the second ":" and what follows it, i don't understand.

2
In UnitHandler.h :

Code: Select all

...
std::list<CUnit*>::iterator slowUpdateIterator;
...
Is the "::" defining that slowUpdateIterator is a variable of type ... something derived of something? And the <CUnit*> looks to me just about as much as chinese...

If anyone cares to explain those, i would be gratefull.
0 x

User avatar
munch
Posts: 311
Joined: 26 May 2005, 20:00

Initialiser list

Post by munch » 19 Nov 2005, 00:06

Let me summarise your post "I am new to C++ can somebody teach me please" =)

1. This is called a constructor - it's what gets called when you create an instance of a class. Every class has at least one constructor (even if it's invisible). Constructors look just like ordinary methods except 1/ they have no given return type (they return an instance of the class), 2/ they are named after the class and 3/ they can have initialiser lists.

The list after the colon is an initialiser list and it's used to set initial values for members of the class.

2. This looks like Chinese to you because it's slightly more advanced. What you have here are scoping operators and templates.

C++ is a big nasty complex language. Even when you understand all the rules that make it work, there are still lots of pitfalls to fall into, because the language rules interact with each other in undesirable ways, meaning that you intuition can't be trusted.

C++ takes a lot of time to learn. Either decide to invest the time in learning it, or give up now. There isn't a quick fix. There are loads of excellent C++ learning resources available for free on-line, including those listed in the Wiki: http://taspring.clan-sy.com/wiki/Source ... ng_C.2B.2B
Beware of short tutorials - they might promise you a quick way in, but they just don't teach you anything worthwhile. Certainly not enough to understand the Spring source code. The shortest course/book I'd recommend is "C++ in 21 Days", which is easy to read and a very good text - it teaches you the minimum you can get away with. It was the first C++ book I read and gave me an excellent introduction.

http://newdata.box.sk/bx/c/

Now go read a C++ manual - I don't intend to drip feed C++ lessons on this board =)

Cheers

Munch
0 x

b1ind
Posts: 48
Joined: 21 Apr 2005, 04:01

Post by b1ind » 19 Nov 2005, 00:50

For your second question it means the following. std:: is a namespace used by the Standard Template Library (STL). Therefore, std::list is the class list in the std namespace. When you use an stl container (list list, vector, etc..), you have to declare what the list will hold. std::list<CUnit*> is therefore a list of pointers to CUnit objects. The final ::iterator means that we want an iterator (an object to loop through a set) for a list of pointers to CUnit. std::list::iterator is a child class of std::list. Each STL container defines its own iterator, but they all have the same base interface.

Hope this helps.
0 x

User avatar
SwiftSpear
Classic Community Lead
Posts: 7287
Joined: 12 Aug 2005, 09:29

Re: Initialiser list

Post by SwiftSpear » 19 Nov 2005, 09:50

munch wrote:I don't know the answer to your question so RTFM
Gees, man, no need to be a dick about it.
0 x

Post Reply

Return to “Engine”