Hey!
So I'm trying to write a game (https://gitlab.com/AleksiyCODE/riftedtimes (not much to look at yet)), but here is a quick question:
when i do in a constructor
class A
{
public:
A(Graphics& in_gfx)
:
gfx(in_gfx) {}; //this line!
privare:
Graphics& gfx;
}
it works, but if i do
class A
{
public:
A(Graphics& in_gfx)
:
{
gfx=in_gfx; //here
};
privare:
Graphics& gfx;
}
it says some shit about deleted functions (i can see that rhey are deleted in Graphics.h)
So, I don't really understand what dark magic initializer list uses, and thus can't replicate it.
p.s. i think i can get away with using pointers, but understanding this seems usefull, so thanks for your answers in advance oмo
A quick question on reference initialisation
- AleksiyCODE
- Posts: 32
- Joined: September 21st, 2019, 8:47 pm
A quick question on reference initialisation
I like ass
Re: A quick question on reference initialisation
Did you copy that code or like type it out again. Because privare is not a C++ keyword
Chili
- AleksiyCODE
- Posts: 32
- Joined: September 21st, 2019, 8:47 pm
Re: A quick question on reference initialisation
Man, you got me there . Here's a copypasted version of initializing Graphics& that works:
class ActionBox
{
public:
ActionBox(const std::wstring & boxSpriteFileName,Surface* iconSprites,
Vec2 in_pos,std::vector<Vec2> in_skillPos, std::vector<char> keyToSkill,
int nBoxSkills,Graphics& in_gfx)
:
boxPos(in_pos),
skillPos(in_skillPos),
keyToSkill(keyToSkill),
//boxPool(pool),
nBoxSkills(nBoxSkills),
pIconSprites(iconSprites),
gfx(in_gfx), //!this ine here
boxSprite(Surface::FromFile(boxSpriteFileName))
{}
protected:
std::vector<Skill*> boxPool;
private:
Vec2 boxPos;
int nBoxSkills;
Surface boxSprite;
Surface* pIconSprites;
std::vector<Skill*> pickedSkills;
Graphics& gfx;
//Pool& boxPool;
std::vector<Vec2> skillPos;
std::vector<char> keyToSkill;
};
and here is the one that doesn't (screenshot from previous message shows the error)
class ActionBox
{
public:
ActionBox(const std::wstring & boxSpriteFileName,Surface* iconSprites,
Vec2 in_pos,std::vector<Vec2> in_skillPos, std::vector<char> keyToSkill,
int nBoxSkills,Graphics& in_gfx)
:
boxPos(in_pos),
skillPos(in_skillPos),
keyToSkill(keyToSkill),
//boxPool(pool),
nBoxSkills(nBoxSkills),
pIconSprites(iconSprites),
boxSprite(Surface::FromFile(boxSpriteFileName))
{
gfx=in_gfx; //here
}
protected:
std::vector<Skill*> boxPool;
private:
Vec2 boxPos;
int nBoxSkills;
Surface boxSprite;
Surface* pIconSprites;
std::vector<Skill*> pickedSkills;
Graphics& gfx;
//Pool& boxPool;
std::vector<Vec2> skillPos;
std::vector<char> keyToSkill;
};
The question is: how else (besides initializer list) can i initialize my Graphics&?
class ActionBox
{
public:
ActionBox(const std::wstring & boxSpriteFileName,Surface* iconSprites,
Vec2 in_pos,std::vector<Vec2> in_skillPos, std::vector<char> keyToSkill,
int nBoxSkills,Graphics& in_gfx)
:
boxPos(in_pos),
skillPos(in_skillPos),
keyToSkill(keyToSkill),
//boxPool(pool),
nBoxSkills(nBoxSkills),
pIconSprites(iconSprites),
gfx(in_gfx), //!this ine here
boxSprite(Surface::FromFile(boxSpriteFileName))
{}
protected:
std::vector<Skill*> boxPool;
private:
Vec2 boxPos;
int nBoxSkills;
Surface boxSprite;
Surface* pIconSprites;
std::vector<Skill*> pickedSkills;
Graphics& gfx;
//Pool& boxPool;
std::vector<Vec2> skillPos;
std::vector<char> keyToSkill;
};
and here is the one that doesn't (screenshot from previous message shows the error)
class ActionBox
{
public:
ActionBox(const std::wstring & boxSpriteFileName,Surface* iconSprites,
Vec2 in_pos,std::vector<Vec2> in_skillPos, std::vector<char> keyToSkill,
int nBoxSkills,Graphics& in_gfx)
:
boxPos(in_pos),
skillPos(in_skillPos),
keyToSkill(keyToSkill),
//boxPool(pool),
nBoxSkills(nBoxSkills),
pIconSprites(iconSprites),
boxSprite(Surface::FromFile(boxSpriteFileName))
{
gfx=in_gfx; //here
}
protected:
std::vector<Skill*> boxPool;
private:
Vec2 boxPos;
int nBoxSkills;
Surface boxSprite;
Surface* pIconSprites;
std::vector<Skill*> pickedSkills;
Graphics& gfx;
//Pool& boxPool;
std::vector<Vec2> skillPos;
std::vector<char> keyToSkill;
};
The question is: how else (besides initializer list) can i initialize my Graphics&?
I like ass
- AleksiyCODE
- Posts: 32
- Joined: September 21st, 2019, 8:47 pm
Re: A quick question on reference initialisation
Oh, forgot to mention that Graphics class is the same as in Beginner series, but Ill leave Graphics.h code here just in case
Spoiler:
I like ass
Re: A quick question on reference initialisation
References must know what they're referencing when they are created. This is why they need to go in the initializer list. Stuff that get its value in the body of the constructor are actually first default constructed and then copy assigned.
For this reason it's actually more efficient to initialize objects in the initializer list anyways since it's just a call to the copy constructor instead of both the default constructor and copy assignment.
Here's an experiment you can do
For this reason it's actually more efficient to initialize objects in the initializer list anyways since it's just a call to the copy constructor instead of both the default constructor and copy assignment.
Here's an experiment you can do
Code: Select all
class Foo
{
public:
Foo() = delete;
};
class Bar
{
public:
Bar( Foo f )
//: foo( f ) <- This will compile
{
foo = f; // This wont compile
}
private:
Foo foo;
}
- AleksiyCODE
- Posts: 32
- Joined: September 21st, 2019, 8:47 pm
Re: A quick question on reference initialisation
Thanks a lot, that fully answers my question!
Now im a step closer to the Almighty C++ Sex God
Now im a step closer to the Almighty C++ Sex God
Spoiler:
I like ass