Jump to content

some pointers


Guest ilija

Recommended Posts

Hey maybe this is a bit offtopic but i want to know how -> exactly works , i know it is same as x->y = *x.y. So what it does the memory adress revers to a class and that active a function or something like that. But before that you first need to create that pointer to the class. Could it be done with using : testclass *x; // your class name is testclass

Or need to add new x; to memory. Because some tutorials says that using new command you can have big change to get memory leaks if you forget delete command after it finished.

Just asking this because -> is alot used in mangos.

Btw couldnt it be just something like this

class blabla
{ public:
void test()
{
...
}

}

int main()
{
blabla f;

f.test();

}

but i think it is in all cases this:

class blabla
{ public:
void test()
{
...
}

}
int main()
{
     blabla *x;
     new x;                     //something like that , still in learning fase of creating pointers
    x->test();
}

Thank you very much

Link to comment
Share on other sites

Hey maybe this is a bit offtopic but i want to know how -> exactly works , i know it is same as x->y = *x.y. So what it does the memory adress revers to a class and that active a function or something like that. But before that you first need to create that pointer to the class. Could it be done with using : testclass *x; // your class name is testclass

Or need to add new x; to memory. Because some tutorials says that using new command you can have big change to get memory leaks if you forget delete command after it finished.

Just asking this because -> is alot used in mangos.

Pointers are probably the most difficult concept to grasp when learning C/C++, however they are surprisingly simple with practice and experience.

You can create a pointer to any variable. You can create a pointer to an instance of a class, an integer, a floating-point number, etc. Even pointers to pointers are allowed.

Here is an example of creating a pointer to an object:

Object object;
Object* objectPointer;
objectPointer = &object;

The important thing to remember is that a pointer is just a memory address (i.e. an integer, 32-bits on 32-bit computer, 64-bits on 64-bit computer, etc.) & gets this memory address from a variable and you can put this into a pointer.

Now let's say I wanted to call a member of each:

object.function(x);
objectPointer->function(x);
(*objectPointer).function(x);

The second and third are exactly the same thing. In the second and third case, it just gets the object being pointed at by the pointer ("object", and this process is called dereferencing) and then uses it like it normally would. The -> operator simply combines the dereferencing (*stuff) and function calling into one operator.

Now, the other thing you wanted to know about is the new operator. You must know already about variable scope. When a function returns, any variables created in that function are automatically deleted. Consider this:

void a_function()
{
   int x;
   Object object;
   x = 3;
   object.function();
   std::cout << x;
}

This will print out the value of x and then return. But as soon as it returns, x and object are deleted. Let's say I wanted it to not delete object when it returns. One way of doing this is to create the variable with the new operator. Then, the variable will be retained until I explicitly delete it.

void a_function()
{
   int x;
   Object * objectPointer = new Object;
   x = 3;
   objectPointer->function();
   std::cout << x;
}

Good, now object will not be deleted. There is one problem though: object won't be deleted until the program exits, so every time the function is called, more memory is taken up and never freed! Also interesting to note is that although the actual Object we created will be stored until it is deleted, the pointer to it, objectPointer, will be deleted upon the function returning. Since this is the only way to find the Object stored in memory, we can't find it anymore and it is lost forever! (until the program exits)

One thing to note though: when you create something with new, it always returns a pointer to the object, rather than the object itself.

Another thing to note is that when you create a variable in the first example, it is created on the stack, which is sort of like temporary storage. When you create an object with new, it is instead created on the heap, which is permanent storage.

So, to re-cap:

Using normal declaration a variable:

- created on the stack (temporary storage)

- deleted when the function returns, or the object it is a member of is deleted (automatically deleted)

- returns the object itself

Using new to create the variable:

- created on the heap ("permanent" storage)

- delete only when you call delete on a pointer to it (or the program exits) (manually deleted)

- returns a pointer to the object

Link to comment
Share on other sites

Thanks for the info but i just wondered , is mangos using alot of heap crap?

my question was about why you use pointers if you also can use declaration like int x;

something like this then method x;

and to call a function in the method do use x.function().

But as i understand , mangos first create pointer to methodes and then use -> instead of .

Abit of topic but what is most important code in mangos( i mean with methods or functions or something else?)

Sorry for my english,

Thnx!

Link to comment
Share on other sites

Mangos does use the heap quite a bit. The reason you can declare instances of objects like you would int is because this is creating them on the stack (temporarily storage) like any other variable (if you want an int to not be deleted, you need to create it on the heap too with new int)

Pointers are also important due to the way C++ calls functions. If I have a function like this:

void function(Object object)
{
   object.x = 5;
}

this means that every time I call this object, C++ will make a copy of the object I pass to it. This copy will be deleted once the function returns (function arguments are in the stack) but the original object will remain unmodified, since only the x of the copy was changed.

To get around this, you can either use the & sign. This forces C++ to pass the original variable instead of a copy:

void function(Object &object)
{
   object.x = 5;
}

However, this still isn't very efficient, because an Object might be a huge data construct! Instead, you can pass a pointer:

void function(Object * object)
{
   object->x = 5;
}

Since a pointer is really just a number, this is very quick.

Link to comment
Share on other sites

it reduces memory usage in the long run because you're not recreating a variable every time but instead passing a reference (ie why it's called a "pointer") to the object so you don't have to recreate it every time. It took me a while to understand it but it's relatively easy. You just have to be careful where you use it and to delete any un-used objects otherwise you get a lot of memory leaks. And pointers to pointers can be dangerous because if you delete the original pointer then the new pointer points to a reference in memory that no longer exists and causes a crash.

Link to comment
Share on other sites

Could you explain this more please?

void function(Object * object)
{
   object->x = 5;
}

i think it is something like that?

class test

{

public:

void function(Object * object)

{

}

};

int main()

{

int x = 9;

test Test;

test * pTest = &Test;

pTest->functie(x)

}

If this is correct why you can do this : test * pTest = &test;

Link to comment
Share on other sites

Could you explain this more please?

void function(Object * object)
{
   object->x = 5;
}

i think it is something like that?

class test

{

public:

void function(Object * object)

{

}

};

int main()

{

int x = 9;

test Test;

test * pTest = &Test;

pTest->functie(x)

}

If this is correct why you can do this : test * pTest = &test;

As far as C is concerned, you can't. It'll throw a compilation error about undeclared variable. Simply because "test" is a data type (class), not actual variable (and thus you can't get it's address).

Link to comment
Share on other sites

I understand the pointers now but i dont know when you have to create a pointer , where should i create a pointer in a small project? Or were are all pointers created in mangos?

Some other question , when i should use struct / class or some others objects?

Link to comment
Share on other sites

xeross155 , i know that , but where are the all pointers in mangos project created ? In some file or someting?

And if you have a big project , how can you create pointers that arent destroyed when the scope ends, so maybe in constructer of a struct or class?

example:

pEffect SpellEffects[TOTAL_SPELL_EFFECTS]=
{
   &Spell::EffectNULL,                                     //  0
   &Spell::EffectInstaKill,                                //  1 SPELL_EFFECT_INSTAKILL
   &Spell::EffectSchoolDMG,                                //  2 SPELL_EFFECT_SCHOOL_DAMAGE
   &Spell::EffectDummy,                                    //  3 SPELL_EFFECT_DUMMY
   &Spell::EffectUnused,                                   //  4 SPELL_EFFECT_PORTAL_TELEPORT          unused from pre-1.2.1
   &Spell::EffectTeleportUnits,                            //  5 SPELL_EFFECT_TELEPORT_UNITS
   &Spell::EffectApplyAura,                                //  6 SPELL_EFFECT_APPLY_AURA
   &Spell::EffectEnvironmentalDMG,                         //  7 SPELL_EFFECT_ENVIRONMENTAL_DAMAGE
   &Spell::EffectPowerDrain,                               //  8 SPELL_EFFECT_POWER_DRAIN
   &Spell::EffectHealthLeech,                              //  9 SPELL_EFFECT_HEALTH_LEECH
   &Spell::EffectHeal,                                     // 10 SPELL_EFFECT_HEAL
   &Spell::EffectBind,                                     // 11 SPELL_EFFECT_BIND
   &Spell::EffectUnused,                                   // 12 SPELL_EFFECT_PORTAL                   unused from pre-1.2.1, exist 2 spell, but not exist any data about its real usage
   &Spell::EffectUnused,                                   // 13 SPELL_EFFECT_RITUAL_BASE              unused from pre-1.2.1
   &Spell::EffectUnused,                                   // 14 SPELL_EFFECT_RITUAL_SPECIALIZE        unused from pre-1.2.1
   &Spell::EffectUnused,                                   // 15 SPELL_EFFECT_RITUAL_ACTIVATE_PORTAL   unused from pre-1.2.1
   &Spell::EffectQuestComplete,                            // 16 SPELL_EFFECT_QUEST_COMPLETE
   &Spell::EffectWeaponDmg,                                // 17 SPELL_EFFECT_WEAPON_DAMAGE_NOSCHOOL
   &Spell::EffectResurrect,                                // 18 SPELL_EFFECT_RESURRECT
   &Spell::EffectAddExtraAttacks,                          // 19 SPELL_EFFECT_ADD_EXTRA_ATTACKS
   &Spell::EffectEmpty,                                    // 20 SPELL_EFFECT_DODGE                    one spell: Dodge
   &Spell::EffectEmpty,                                    // 21 SPELL_EFFECT_EVADE                    one spell: Evade (DND)
   &Spell::EffectParry,                                    // 22 SPELL_EFFECT_PARRY
   &Spell::EffectBlock,                                    // 23 SPELL_EFFECT_BLOCK                    one spell: Block
   &Spell::EffectCreateItem,                               // 24 SPELL_EFFECT_CREATE_ITEM
   &Spell::EffectEmpty,                                    // 25 SPELL_EFFECT_WEAPON                   spell per weapon type, in ItemSubclassmask store mask that can be used for usability check at equip, but current way using skill also work.
   &Spell::EffectEmpty,                                    // 26 SPELL_EFFECT_DEFENSE                  one spell: Defense
   &Spell::EffectPersistentAA,                             // 27 SPELL_EFFECT_PERSISTENT_AREA_AURA
   &Spell::EffectSummonType,                               // 28 SPELL_EFFECT_SUMMON
   &Spell::EffectLeapForward,                              // 29 SPELL_EFFECT_LEAP
   &Spell::EffectEnergize,                                 // 30 SPELL_EFFECT_ENERGIZE
   &Spell::EffectWeaponDmg,                                // 31 SPELL_EFFECT_WEAPON_PERCENT_DAMAGE
   &Spell::EffectTriggerMissileSpell,                      // 32 SPELL_EFFECT_TRIGGER_MISSILE
   &Spell::EffectOpenLock,                                 // 33 SPELL_EFFECT_OPEN_LOCK
   &Spell::EffectSummonChangeItem,                         // 34 SPELL_EFFECT_SUMMON_CHANGE_ITEM
   &Spell::EffectApplyAreaAura,                            // 35 SPELL_EFFECT_APPLY_AREA_AURA_PARTY
   &Spell::EffectLearnSpell,                               // 36 SPELL_EFFECT_LEARN_SPELL
   &Spell::EffectEmpty,                                    // 37 SPELL_EFFECT_SPELL_DEFENSE            one spell: SPELLDEFENSE (DND)
   &Spell::EffectDispel,                                   // 38 SPELL_EFFECT_DISPEL
   &Spell::EffectEmpty,                                    // 39 SPELL_EFFECT_LANGUAGE                 misc store lang id
   &Spell::EffectDualWield,                                // 40 SPELL_EFFECT_DUAL_WIELD
   &Spell::EffectJump,                                     // 41 SPELL_EFFECT_JUMP
   &Spell::EffectJump,                                     // 42 SPELL_EFFECT_JUMP2
   &Spell::EffectTeleUnitsFaceCaster,                      // 43 SPELL_EFFECT_TELEPORT_UNITS_FACE_CASTER
   &Spell::EffectLearnSkill,                               // 44 SPELL_EFFECT_SKILL_STEP
   &Spell::EffectAddHonor,                                 // 45 SPELL_EFFECT_ADD_HONOR                honor/pvp related
   &Spell::EffectNULL,                                     // 46 SPELL_EFFECT_SPAWN                    spawn/login animation, expected by spawn unit cast, also base points store some dynflags
   &Spell::EffectTradeSkill,                               // 47 SPELL_EFFECT_TRADE_SKILL
   &Spell::EffectUnused,                                   // 48 SPELL_EFFECT_STEALTH                  one spell: Base Stealth
   &Spell::EffectUnused,                                   // 49 SPELL_EFFECT_DETECT                   one spell: Detect
   &Spell::EffectTransmitted,                              // 50 SPELL_EFFECT_TRANS_DOOR
   &Spell::EffectUnused,                                   // 51 SPELL_EFFECT_FORCE_CRITICAL_HIT       unused from pre-1.2.1
   &Spell::EffectUnused,                                   // 52 SPELL_EFFECT_GUARANTEE_HIT            unused from pre-1.2.1
   &Spell::EffectEnchantItemPerm,                          // 53 SPELL_EFFECT_ENCHANT_ITEM
   &Spell::EffectEnchantItemTmp,                           // 54 SPELL_EFFECT_ENCHANT_ITEM_TEMPORARY
   &Spell::EffectTameCreature,                             // 55 SPELL_EFFECT_TAMECREATURE
   &Spell::EffectSummonPet,                                // 56 SPELL_EFFECT_SUMMON_PET
   &Spell::EffectLearnPetSpell,                            // 57 SPELL_EFFECT_LEARN_PET_SPELL
   &Spell::EffectWeaponDmg,                                // 58 SPELL_EFFECT_WEAPON_DAMAGE
   &Spell::EffectCreateRandomItem,                         // 59 SPELL_EFFECT_CREATE_RANDOM_ITEM       create item base at spell specific loot
   &Spell::EffectProficiency,                              // 60 SPELL_EFFECT_PROFICIENCY
   &Spell::EffectSendEvent,                                // 61 SPELL_EFFECT_SEND_EVENT
   &Spell::EffectPowerBurn,                                // 62 SPELL_EFFECT_POWER_BURN
   &Spell::EffectThreat,                                   // 63 SPELL_EFFECT_THREAT
   &Spell::EffectTriggerSpell,                             // 64 SPELL_EFFECT_TRIGGER_SPELL
   &Spell::EffectApplyAreaAura,                            // 65 SPELL_EFFECT_APPLY_AREA_AURA_RAID
   &Spell::EffectRestoreItemCharges,                       // 66 SPELL_EFFECT_RESTORE_ITEM_CHARGES     itemtype - is affected item ID
   &Spell::EffectHealMaxHealth,                            // 67 SPELL_EFFECT_HEAL_MAX_HEALTH
   &Spell::EffectInterruptCast,                            // 68 SPELL_EFFECT_INTERRUPT_CAST
   &Spell::EffectDistract,                                 // 69 SPELL_EFFECT_DISTRACT
   &Spell::EffectPull,                                     // 70 SPELL_EFFECT_PULL                     one spell: Distract Move
   &Spell::EffectPickPocket,                               // 71 SPELL_EFFECT_PICKPOCKET
   &Spell::EffectAddFarsight,                              // 72 SPELL_EFFECT_ADD_FARSIGHT
   &Spell::EffectNULL,                                     // 73 SPELL_EFFECT_UNTRAIN_TALENTS          one spell: Trainer: Untrain Talents
   &Spell::EffectApplyGlyph,                               // 74 SPELL_EFFECT_APPLY_GLYPH
   &Spell::EffectHealMechanical,                           // 75 SPELL_EFFECT_HEAL_MECHANICAL          one spell: Mechanical Patch Kit
   &Spell::EffectSummonObjectWild,                         // 76 SPELL_EFFECT_SUMMON_OBJECT_WILD
   &Spell::EffectScriptEffect,                             // 77 SPELL_EFFECT_SCRIPT_EFFECT
   &Spell::EffectUnused,                                   // 78 SPELL_EFFECT_ATTACK
   &Spell::EffectSanctuary,                                // 79 SPELL_EFFECT_SANCTUARY
   &Spell::EffectAddComboPoints,                           // 80 SPELL_EFFECT_ADD_COMBO_POINTS
   &Spell::EffectUnused,                                   // 81 SPELL_EFFECT_CREATE_HOUSE             one spell: Create House (TEST)
   &Spell::EffectNULL,                                     // 82 SPELL_EFFECT_BIND_SIGHT
   &Spell::EffectDuel,                                     // 83 SPELL_EFFECT_DUEL
   &Spell::EffectStuck,                                    // 84 SPELL_EFFECT_STUCK
   &Spell::EffectSummonPlayer,                             // 85 SPELL_EFFECT_SUMMON_PLAYER
   &Spell::EffectActivateObject,                           // 86 SPELL_EFFECT_ACTIVATE_OBJECT
   &Spell::EffectNULL,                                     // 87 SPELL_EFFECT_WMO_DAMAGE (57 spells in 3.3.2)
   &Spell::EffectNULL,                                     // 88 SPELL_EFFECT_WMO_REPAIR (2 spells in 3.3.2)
   &Spell::EffectNULL,                                     // 89 SPELL_EFFECT_WMO_CHANGE (7 spells in 3.3.2)
   &Spell::EffectKillCreditPersonal,                       // 90 SPELL_EFFECT_KILL_CREDIT              Kill credit but only for single person
   &Spell::EffectUnused,                                   // 91 SPELL_EFFECT_THREAT_ALL               one spell: zzOLDBrainwash
   &Spell::EffectEnchantHeldItem,                          // 92 SPELL_EFFECT_ENCHANT_HELD_ITEM
   &Spell::EffectUnused,                                   // 93 SPELL_EFFECT_93 (old SPELL_EFFECT_SUMMON_PHANTASM)
   &Spell::EffectSelfResurrect,                            // 94 SPELL_EFFECT_SELF_RESURRECT
   &Spell::EffectSkinning,                                 // 95 SPELL_EFFECT_SKINNING
   &Spell::EffectCharge,                                   // 96 SPELL_EFFECT_CHARGE
   &Spell::EffectSummonAllTotems,                          // 97 SPELL_EFFECT_SUMMON_ALL_TOTEMS
   &Spell::EffectKnockBack,                                // 98 SPELL_EFFECT_KNOCK_BACK
   &Spell::EffectDisEnchant,                               // 99 SPELL_EFFECT_DISENCHANT
   &Spell::EffectInebriate,                                //100 SPELL_EFFECT_INEBRIATE
   &Spell::EffectFeedPet,                                  //101 SPELL_EFFECT_FEED_PET
   &Spell::EffectDismissPet,                               //102 SPELL_EFFECT_DISMISS_PET
   &Spell::EffectReputation,                               //103 SPELL_EFFECT_REPUTATION
   &Spell::EffectSummonObject,                             //104 SPELL_EFFECT_SUMMON_OBJECT_SLOT1
   &Spell::EffectSummonObject,                             //105 SPELL_EFFECT_SUMMON_OBJECT_SLOT2
   &Spell::EffectSummonObject,                             //106 SPELL_EFFECT_SUMMON_OBJECT_SLOT3
   &Spell::EffectSummonObject,                             //107 SPELL_EFFECT_SUMMON_OBJECT_SLOT4
   &Spell::EffectDispelMechanic,                           //108 SPELL_EFFECT_DISPEL_MECHANIC
   &Spell::EffectSummonDeadPet,                            //109 SPELL_EFFECT_SUMMON_DEAD_PET
   &Spell::EffectDestroyAllTotems,                         //110 SPELL_EFFECT_DESTROY_ALL_TOTEMS
   &Spell::EffectDurabilityDamage,                         //111 SPELL_EFFECT_DURABILITY_DAMAGE
   &Spell::EffectUnused,                                   //112 SPELL_EFFECT_112 (old SPELL_EFFECT_SUMMON_DEMON)
   &Spell::EffectResurrectNew,                             //113 SPELL_EFFECT_RESURRECT_NEW
   &Spell::EffectTaunt,                                    //114 SPELL_EFFECT_ATTACK_ME
   &Spell::EffectDurabilityDamagePCT,                      //115 SPELL_EFFECT_DURABILITY_DAMAGE_PCT
   &Spell::EffectSkinPlayerCorpse,                         //116 SPELL_EFFECT_SKIN_PLAYER_CORPSE       one spell: Remove Insignia, bg usage, required special corpse flags...
   &Spell::EffectSpiritHeal,                               //117 SPELL_EFFECT_SPIRIT_HEAL              one spell: Spirit Heal
   &Spell::EffectSkill,                                    //118 SPELL_EFFECT_SKILL                    professions and more
   &Spell::EffectApplyAreaAura,                            //119 SPELL_EFFECT_APPLY_AREA_AURA_PET
   &Spell::EffectUnused,                                   //120 SPELL_EFFECT_TELEPORT_GRAVEYARD       one spell: Graveyard Teleport Test
   &Spell::EffectWeaponDmg,                                //121 SPELL_EFFECT_NORMALIZED_WEAPON_DMG
   &Spell::EffectUnused,                                   //122 SPELL_EFFECT_122                      unused
   &Spell::EffectSendTaxi,                                 //123 SPELL_EFFECT_SEND_TAXI                taxi/flight related (misc value is taxi path id)
   &Spell::EffectPlayerPull,                               //124 SPELL_EFFECT_PLAYER_PULL              opposite of knockback effect (pulls player twoard caster)
   &Spell::EffectModifyThreatPercent,                      //125 SPELL_EFFECT_MODIFY_THREAT_PERCENT
   &Spell::EffectStealBeneficialBuff,                      //126 SPELL_EFFECT_STEAL_BENEFICIAL_BUFF    spell steal effect?
   &Spell::EffectProspecting,                              //127 SPELL_EFFECT_PROSPECTING              Prospecting spell
   &Spell::EffectApplyAreaAura,                            //128 SPELL_EFFECT_APPLY_AREA_AURA_FRIEND
   &Spell::EffectApplyAreaAura,                            //129 SPELL_EFFECT_APPLY_AREA_AURA_ENEMY
   &Spell::EffectNULL,                                     //130 SPELL_EFFECT_REDIRECT_THREAT
   &Spell::EffectUnused,                                   //131 SPELL_EFFECT_131                      used in some test spells
   &Spell::EffectPlayMusic,                                //132 SPELL_EFFECT_PLAY_MUSIC               sound id in misc value (SoundEntries.dbc)
   &Spell::EffectUnlearnSpecialization,                    //133 SPELL_EFFECT_UNLEARN_SPECIALIZATION   unlearn profession specialization
   &Spell::EffectKillCredit,                               //134 SPELL_EFFECT_KILL_CREDIT              misc value is creature entry
   &Spell::EffectNULL,                                     //135 SPELL_EFFECT_CALL_PET
   &Spell::EffectHealPct,                                  //136 SPELL_EFFECT_HEAL_PCT
   &Spell::EffectEnergisePct,                              //137 SPELL_EFFECT_ENERGIZE_PCT
   &Spell::EffectLeapBack,                                 //138 SPELL_EFFECT_LEAP_BACK                Leap back
   &Spell::EffectNULL,                                     //139 SPELL_EFFECT_CLEAR_QUEST              (misc - is quest ID)
   &Spell::EffectForceCast,                                //140 SPELL_EFFECT_FORCE_CAST
   &Spell::EffectNULL,                                     //141 SPELL_EFFECT_141                      damage and reduce speed?
   &Spell::EffectTriggerSpellWithValue,                    //142 SPELL_EFFECT_TRIGGER_SPELL_WITH_VALUE
   &Spell::EffectApplyAreaAura,                            //143 SPELL_EFFECT_APPLY_AREA_AURA_OWNER
   &Spell::EffectNULL,                                     //144 SPELL_EFFECT_144                      Spectral Blast
   &Spell::EffectNULL,                                     //145 SPELL_EFFECT_145                      Black Hole Effect
   &Spell::EffectActivateRune,                             //146 SPELL_EFFECT_ACTIVATE_RUNE
   &Spell::EffectQuestFail,                                //147 SPELL_EFFECT_QUEST_FAIL               quest fail
   &Spell::EffectNULL,                                     //148 SPELL_EFFECT_148                      single spell: Inflicts Fire damage to an enemy.
   &Spell::EffectCharge2,                                  //149 SPELL_EFFECT_CHARGE2                  swoop
   &Spell::EffectNULL,                                     //150 SPELL_EFFECT_150                      2 spells in 3.3.2
   &Spell::EffectTriggerRitualOfSummoning,                 //151 SPELL_EFFECT_TRIGGER_SPELL_2
   &Spell::EffectNULL,                                     //152 SPELL_EFFECT_152                      summon Refer-a-Friend
   &Spell::EffectNULL,                                     //153 SPELL_EFFECT_CREATE_PET               misc value is creature entry
   &Spell::EffectTeachTaxiNode,                            //154 SPELL_EFFECT_TEACH_TAXI_NODE          single spell: Teach River's Heart Taxi Path
   &Spell::EffectTitanGrip,                                //155 SPELL_EFFECT_TITAN_GRIP Allows you to equip two-handed axes, maces and swords in one hand, but you attack $49152s1% slower than normal.
   &Spell::EffectEnchantItemPrismatic,                     //156 SPELL_EFFECT_ENCHANT_ITEM_PRISMATIC
   &Spell::EffectCreateItem2,                              //157 SPELL_EFFECT_CREATE_ITEM_2            create item or create item template and replace by some randon spell loot item
   &Spell::EffectMilling,                                  //158 SPELL_EFFECT_MILLING                  milling
   &Spell::EffectRenamePet,                                //159 SPELL_EFFECT_ALLOW_RENAME_PET         allow rename pet once again
   &Spell::EffectNULL,                                     //160 SPELL_EFFECT_160                      single spell: Nerub'ar Web Random Unit
   &Spell::EffectSpecCount,                                //161 SPELL_EFFECT_TALENT_SPEC_COUNT        second talent spec (learn/revert)
   &Spell::EffectActivateSpec,                             //162 SPELL_EFFECT_TALENT_SPEC_SELECT       activate primary/secondary spec
};

As i understand it create a arrays that are pointed to some functions in Spell object?

Other example:

   if(!unitTarget || unitTarget->isAlive())
       return;

You see that the unitTarget->isAlive()

Goes to method and do function isAlive.

but what it says is this: *unitTarget.isAlive()

but where is pointer created? Should be something like this:

unitTarget f;
unitTarget * unitTarget = &f;

But where the pointer of that is created , this is all i want to know :)

Link to comment
Share on other sites

SpellEffects[] is an array of function pointers, indeed.

They're a bit of a special case, you don't allocate or delete functions, they are tied to the binary file that contain their code. They're more common in C (no virtual member functions), but still can be useful in C++, like in this case to map an integer to a function in an efficient way.

As for

(1) unitTarget->isAlive();

vs.

(2) *unitTarget.isAlive();

they are not the same, because . operator has higher precedence than *, so the equivalence to (1) is (*unitTarget).isAlive();

how can you create pointers that arent destroyed when the scope ends, so maybe in constructer of a struct or class?

The pointer itself is by definition destroyed when its scope ends, but you are probably refering to the memory it points to. Of course if the object it points to goes out of scope, the pointer becomes invalid (i.e. dereferencing likely crashes the application, or at least does not give you a valid object instance).

As already said, pointers are commonly involved when doing dynamic memory allocation, i.e. with new and delete.

new returns a pointer to a newly allocated object, and this pointer (the address it holds, to be super-clear) is valid until you call delete for this very same address. And if the last pointer holding that address goes out of scope, you successfully created a memory leak :)

And yes, constructors/destructors are places where you'll often find new/delete to allocate/free variable sized class members, and common places to leak memory...

Link to comment
Share on other sites

Pff i still dont understand(maybe through my english) clearly:

you said this

Unit *unitTarget = m_targets.getUnitTarget();

so if m_targets.getUnitTarget(); is a return function , as it is you can do this

cout << m_targets.getUnitTarget(); OR

cout << *unitTarget;

Ok , all i want to know is how and when and where to create a pointer :

Example

#include "stdafx.h"
#include <iostream>

void blabla();

struct test
{
   int calculate1()
   {
       return 9;
   }

};

int main()
{
   test f;
   test *pTest = &f;
   std::cout << pTest->calculate1();
   blabla();
   char xy;
   std::cin >> xy;
   return 0;
}

void blabla()
{
   std::cout << pTest->calculate1();
}

This code has little bug , the function void blabla(); cant read pTest->calculate1();

but how to make a major pointer that all the functions could use that pointer without creating the pointer for each function?

And where to write that? Please a example

Link to comment
Share on other sites

This code has little bug , the function void blabla(); cant read pTest->calculate1();

Of course not, pTest has not been declared anywhere in scope, it can't pull it from nowhere.

This has nothing to do with pointers at all, you probably should go back to the very basics of programming and how to use/pass variables...

but how to make a major pointer that all the functions could use that pointer without creating the pointer for each function?

No idea what you mean, only way to have a variable available to all functions is making them global, but among the very first things a good C/C++ book should teach you is that global variables are bad design in >95% of all cases.

Pass the pointer, period.

Link to comment
Share on other sites

Pff , still not understand how pointers in mangos are working :S(sorry , im noob i think :( ) .

I know how pointers in simple programm working but cant figure it out how in mangos.

Simpel pointers are something like this if your class/struct is called mangos:

mangos *pMangos;

mangos f;

pMangos = &f;

But in mangos project , where the hell are those pointers created?

example :

m_target->GetMap()

this code is from spellAuras.cpp.

Ok now i have been searched everywhere but i cant find anywhere where the pointer m_target is created , i have looked in the header file and found this: Unit* m_target;

But still , it only create a pointer , but it dont allocate it to so something.

I dont understand , im confused , im tearing :'(.

It is just mangos use alot of x->y() (*x.y() ) , and i really need to understand how those pointers are created , because they dont just create in most easiest why with first : mangos f; then mangos *pMangos = &f;

Link to comment
Share on other sites

  • 2 weeks later...
No DasBlub is creating a new class thus he's pointing to that class. And why is it recommended to not use new ?

Because it makes a dynamic memory allocation. Doing it like

mangos *pMangos;
mangos f;
pMangos = &f;

not only removes the need to call "delete", but it creates all the variables directly on function's stack, saving a few kernel mmap() calls.

On the other hand, it's total nonsense to create a pointer to "f" in the same scope (no, it won't speed up anything, it'll even slow things down a bit).

Learning pointers in C++ code might be a lot harder, i'd suggest learning those in C99 instead. No C++ complexity involved + you can easily disassemble the binary and see how it works.

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