Jump to content

C++ operator?


Guest kageb0shi

Recommended Posts

I have a little bit of experience in C++, but evidently not enough. Digging around in Mangos source I keep seeing this operator(?)

->

context example:

                if(m_spellInfo->SpellIconID == 1648)        // Execute
               {
                   if(!m_targets.getUnitTarget() || m_targets.getUnitTarget()->GetHealth() > m_targets.getUnitTarget()->GetMaxHealth()*0.2)
                       return SPELL_FAILED_BAD_TARGETS;
               }

I recognize all the operators in that example, ! = NOT, || = OR, == equals to, > greater than, GetHealth() function, * multiply, ; end of statement, except I do not know what -> is

I have done google searches, but no relevant results.

Link to comment
Share on other sites

ah I see. Thank you so much for helping me understand that.

just as, for example

int a[5] = { 3, 4, 8, 9}

int b = 2;

int value = a;

value would equal 8.

I understand to a certain degree. Though I am certain a pointer is more complex than an array. I had not yet covered pointers in my C++ class. next semester i think I will see it.

Link to comment
Share on other sites

A pointer is sort of what it sounds like: a pointer to a location in memory. That location can be anything. If it is an instance of class, -> can be used to call member variables or functions from that instance of the class.

It sounds much more difficult than it actually is. Just remember than if you have a pointer variable, like Player * player, and you want to call a function on it, you would use player->function().

Link to comment
Share on other sites

pointers are much more efficient to move around.

function doSomething(Player thePlayer); // will copy the Player struct each time it is called, will delete it when it returns, will not modify the original Player if the function modify (or call a member function modifying) thePlayer

function doSomething(Player* thePlayer); // none of the above

so the second version is faster and use less memory.

Link to comment
Share on other sites

pointers are much more efficient to move around.

function doSomething(Player thePlayer); // will copy the Player struct each time it is called, will delete it when it returns, will not modify the original Player if the function modify (or call a member function modifying) thePlayer

function doSomething(Player* thePlayer); // none of the above

so the second version is faster and use less memory.

ah I see. Instead of passing by value (which makes a copy if I understand it) it is actually passing the variable, so then the original variable is modified and not some copy. I see!

Link to comment
Share on other sites

as we are here, i'd like to know _what_ pointer whe should use for exemple on spellauras.cpp.

i understood that m_taget is the taget and caster is the player who do something but...

can i use that?

for exemple if i want the rogue autocast something on him (in this case the find weakness buff) , can i use

if (caster->HasSpell(31242)) //if has talent

caster->CastSpell(caster, 31238, true, NULL, NULL, GetCasterGUID()); //cast on him the spell

thanks for asnwer

Link to comment
Share on other sites

Pointers are probably the most powerful thing in C and pretty important in C++ as well. That means there's a LOT of tutorials explaining pointers from every possible angle of explanation.

IMHO the easiest way of understanding pointers is to realize what they actually are. A four-byte (on 32bit machines) or eight-byte (64bit) locations in memory, containing a destination address they're pointing to. Nothing more. All those types of pointers (like int*, Player* and so on) only allows you to easily manipulate with the destination target. A pointer has still 4 (8) bytes. That's why you actually can use a void* and then typecast it like ((Player *)my_void_ptr)->GetName().

Pointers are simple once you understand how they work.

edit: just an example I used some year back in a Linux kernel module;

char *buffer;
__u16 ip_csum;
__u16 tcp_csum;

buffer = skb_network_header(skb);
ip_csum = ((struct iphdr *)buffer)->check;
tcp_csum = ((struct tcphdr *)buffer+sizeof(struct iphdr))->check;

(I ofc omitted a lots of checks and such)

This way I'm able to get both IPv4 and TCP checksums from a packet using only one generic *buffer pointer (there could also be a check for IPv4/6 and doing all the work with only "buffer").

In practice it's better to use more abstraction layers.

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use