• Advertisement
Sign in to follow this  

Passing an object to constructor of another object? C++

This topic is 1475 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

Hi everyone! I am hoping someone can help me out with this. I've been looking around online all night and I am so confused.
 
I have 2 classes, and 1 sub-class. Spell, Heal::Spell, and Hero;
 
Spell is a friend class of Hero and Heal is a subclass of Spell.
I need Heal to be able to make changes to a Hero object, so I am wanting to pass a pointer to the Hero object to the Heal constructor. 
 
If someone can help me with this I would be so grateful! I will include my code, so far.
 
classes.h
Spell
{
public:
     // code
private:
     Hero &hero;
}
 
Heal(const Hero& h)
{
public:
     // code
protected:
     // code
}
 
classes.cpp
Heal::Heal(const Hero& h): hero(h)
{
     //code
}
Thats basically what I have for the classes. Any clarification on how to fix this???
 

Share this post


Link to post
Share on other sites
Advertisement

I am so thoroughly confused by the syntax you are using. Did you by any chance mean something more like this? 

class Hero
{
	// Awesome and clean code
};

class Spell
{
public:
	// Awesome and clean interface
private:
	friend class Hero; // Ouch, not too awesome

	Hero &hero;
};

class Heal : public Spell
{
public:
	Heal(const Hero&);
	
protected:
	// Helper functions
};

Well, in order to pass a modifiable Hero pointer to Heal, you need to watch out for a couple of things.

 

One, you are passing a const Hero, which means you won't be able to modify it. Remove the const keyword if you want to modify the object.

 

Two, as far as I can tell, you're passing Hero to the wrong object. The member variable hero in Spell is declared private and trying to assign a value to it from Heal will generate a compiler error. 

 

Try fixing those errors and see if you can get it to do what you want.

Edited by boogyman19946

Share this post


Link to post
Share on other sites
You can pass it into Heal's constructor, and Heal can pass it to Spell's constructor:
 
class Spell
{
public:
     Spell(const Hero& h) : hero(h) { }

protected:
     Hero &hero;
};
 
class Heal
{
public:
     Heal(const Hero& h) : Spell(h) { }

};
Though, really, you probably shouldn't pass in Hero until the spell is ready to be used:
class Spell
{
public:
     Spell() { }

     virtual void UseOn(Hero &hero) = 0;
};
 
class Heal
{
public:
     Heal() { }

     void UseOn(Hero &hero) override
     {
        float amountToHeal = float(hero.maxHealth) * 0.25f;
        hero.health = (int)amountToHeal;
     }
};

Share this post


Link to post
Share on other sites

I see, thank you. It seems I have fixed the errors with the classes, but I don't if it's working yet because calling it is giving me an error now.

I removed const, as was mentioned, and I moved Hero& hero to the protected section of Heal. Then I have:

 

 

spells.push_back(new Heal(heroes.at(0)) );

 

spells is a vector made above. std::vector<Spell*> spells;

and heroes is the hero vector. std::vector<Hero*> heroes;

 

This just isn't working now. Am I calling it wrong?

Share this post


Link to post
Share on other sites

error C2664: 'Heal::Heal(Hero &)' : cannot convert parameter 1 from 'Hero **' to 'Hero &'

 

I've tried adding & and * but that doesn't work either.

Share this post


Link to post
Share on other sites
'Hero **' means it's a pointer to a pointer to a Hero. '*expression' will turn it into a pointer to a Hero. '**expression' will turn it into a Hero which the compiler can convert into a reference to a Hero as desired.

However, a double pointer feels like you have gone too far down the rabbit hole. You should seriously consider taking several steps backwards and doing more exercises regarding types.

Share this post


Link to post
Share on other sites

I am concerned about the ownership issues that this constructor is causing. How are you going to ensure the Hero reference remains valid? Can you guarantee that the Hero will not be deleted (presuming it is dynamically allocated), leaving an invalid reference? Servant's alternative approach is probably what you should be doing.

 

You should probably be using smart pointers or values, as it is clear you don't yet understand how raw pointers behave. You'll still need to learn the concepts, but smart pointers at least will help you start with good practises and, used exclusively, hopefully limit the kinds of mistakes that are possible.

Share this post


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

  • Advertisement