Advertisement Jump to content
Sign in to follow this  

Java and C++

This topic is 755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Id like to ask sth about c++ and java.

In java if a class is called Player

i can use

player.SetXp (for example)


public CharInfo(Player cha)

is it possible to do the same in C++ and how?

Edited by Brother Bob

Share this post

Link to post
Share on other sites

In C++ both are possible with the same syntax.


however, if you do some of those things directly in C++, youre lining youreself up for performance issues. How much C++ do you know already?


I'm not sure if the default of java is to pass large objects by reference, but in C++ the default is to pass by value and do a copy, so if you do:


with your definition is above, a copy of "cha" is made using the copy constructor, and then that copy passed to the function. After the function completes, the destructor (if any) of the "cha" object is also called, as the local variable goes out of scope. To avoid this copy, and destruction, and pass by reference instead (this is usually actually what you want in 99% of cases, unless you plan to temporarily change values within the class and aren't bothered about potentially instantiating another object on the stack), use the reference syntax and also optionally set the parameter as "const":

class Something {
    void CharInfo(const Player &cha);

This avoids calling the copy constructor, the "const" prevents accidentally changing the referenced data, and allows for shorthand tricks like:

CharInfo(Player(some, ctor, params));

... which will instantiate a temporary object of type player, passing the parameters "some", "ctor" and "params" into the constructor. Note that in this example the Player class would need a constructor definition which accepts three parameters.


The "." syntax is used when referencing any non-pointer class member, and the "->" syntax for any pointer-to a class, e.g.:

Player p1;
Player* p2 = new Player();
delete p2;

Also remember that C++ is a manually memory managed language, where java is garbage collected, so if you want to allocate new objects you have two ways of doing it. You can either use the first example above which allocates new objects on the local stack (theyre destroyed when they go out of scope) or you can use 'new' and 'delete' operators as above to create pointers to them on the heap, which are then only destroyed when you call delete, regardless of scope.


If you really must use new and delete, rather than just using the stack for variables, then remember to always always delete what you "new". If you don't, then you have what is known as a memory leak, and before you know it, your program is consuming several gigabytes of RAM to do nothing. This is one of the advantages of garbage collected languages like Java as you can just fire and forget. As a new user to C++ i recommend avoiding using new and delete for the time being unless you are absolutely sure you know what you are doing and why, as it's then much easier to track what is allocated and where.


Hope this helps!

Edited by Brain

Share this post

Link to post
Share on other sites

one more think

with this code:

#ifndef PCSTATS_H
#define PCSTATS_H

#include <string>
#include "SkillsStats.h"

class PChar {
const char charname;
const char chartitle;
const char accountusername;
const char password;
std::string email;
int MaxHealthPoints; //Health Points
int MaxManaPoints; //Mana Points
int MaxStaminaPoints; //Stamina Points
int HealthPoints; //Health Points
int ManaPoints; //Mana Points
int StaminaPoints; //Stamina Points
int EvasionPoints; //Evasion Points
int PhysicalDamage; //Physical Damage
int MagicDamage; //Magic Damage
int PhysicalDefence; //Physical Defence
int MagicalDefence; //Magical Defence
int AgilityPoints; //Aglity Points
int AttackSpeed; //Attack Speed
int MagicSpeed; //Casting Speed
int WalkingSpeed; //Walking Speed
int RunningSpeed; //Running Speed
int FlyingSpeed; //Flying Speed
int AttackRange; //Attack Range
int BaseHealthRegen; //Base HP Regeneration
int BaseManaRegen; //Base MP Regeneration
int CoolDownReduction;
int MissPercentage;
int cXP; //Char Xp
int cLevel; //Char LvL
int cID; //Char ID
int cX;
int cY;
int cZ;
int ComboPoints; //Combo Points
int ClassDifficulty; //Class Difficulty
int sex; // 1 - Male / 2 - Female
std::string NameColour; //Name Colour
std::string TitleColour; //Title Colour
int NFriends; //Friend's Number
int faction; //Faction ID
int kingdom; //Kingdom ID
int CthDel; //Global Chat Delay
bool block;
bool inParty;
bool inClan;
bool Jailed;
bool Observer;
bool Dead; //false if alive, true if dead
int ClanID;
int ClassId;
int Level;
int Expirience;
int punished; //Punishment ID , 0 For not being punished
int pndm; //Punishment Time
int PvPPoints; //Player VS Player Points
bool flagged; //This is a check, if the player has attacked an other
bool loggedin;
int hairstyle;
int haircolour;
int ping;
int fps; //fraps per minute
int createtime;
int onlinetime;
int leaveclantime;
int deleteclantime;
int lastaccess;
int xprateboost;
int droprateboost;
bool vip;
bool CanAttack = true;
bool CanCast = true;
// Skills' Effects
bool bleeding;
bool beingHealed;
bool Silenced;
bool Root;
bool Stun;
bool IsSlowed;
bool IsOnAir; //This makes the character but is disabled in the same time too
bool isBlinded; //The Miss % is increased highly
bool isFeared;
bool isCharmed;
bool isTaunted;
bool isRooted;
bool isInStasis; //A unit in stasis is untargetable, but stunned in the same time

when i use this:


void StunTimerStart()
    PChar chara;

im getting error: the default constructor Character cannot be referenced -- it is a deleted function.


Severity    Code    Description    Project    File    Line    Suppression State
Error    C2280    'Character::Character(void)': attempting to reference a deleted function    serverside    c:\users\alex_\desktop\the mysteries of genesis\serverside\serverside\pcinstance.cpp    9    

in the other file


its compiler error c2280

Edited by alextoti99

Share this post

Link to post
Share on other sites

When the default constructor is deleted, it means that some class-variable cannot be default constructed, and you haven't written a custom constructor. Unless its a typo, the problem lies here:

const char charname;
const char chartitle;
const char accountusername;
const char password;

Did you mean const char*, as in a character-array/string? "const char" is a single character, that cannot be changed after the creation of the class. Thus, there cannot be a default constructor - how would the compiler now how to initialize those values, that cannot be possibly changed after the class is created? So probably just fix it to const char*, or std::string, as you are already using directly one line down.


If that still doesn't fix it, write a custom constructor:

class PChar
     PChar() // there will be an error message


then the compiler will tell you exactly which members it cannot default-construct.


EDIT: Don't replace your original question with a new one, the replay was totally enough to know what you wanted. If anything, place an "EDIT: at the bottom at your original question". I was totally confused of Brains answer, till I saw the edit.

Edited by Juliean

Share this post

Link to post
Share on other sites

Original post has been restored and some replies regarding post history has been hidden. You can continue that discussion in the corresponding thread; clicky. Do not fundamentally change you posts like this again. 

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!