Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Little help with code, requiring knowledge.

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

Hello.

I have a base class that has a variable that needs to be set when the class object is made.

But when i inherit the class, i have no idea how to invoke the constructor, what are workarounds?

How can constructor of MainMenu class also invoke Collision constructor?

//I have class like this, its used as a base class
//NOTE its constructor is taking a argument
class Collision

{

public:

    Collision(sf::RenderWindow *RenWin);

    bool Col_RectMouse(sf::RectangleShape &rec);

private:

    sf::RenderWindow *renWin;

};
 
//The functions the above class uses
Collision::Collision(sf::RenderWindow *RenWin)

{

    this->renWin = RenWin;

}



bool Collision::Col_RectMouse(sf::RectangleShape &rec)

{

    int x = sf::Mouse::getPosition(*renWin).x;

    int y = sf::Mouse::getPosition(*renWin).y;

    if(x > rec.getPosition().x &&

       x < rec.getPosition().x + rec.getSize().x &&

       y > rec.getPosition().y &&

       y < rec.getPosition().y + rec.getSize().y)

    {

        return true;//Collision

    }

    return false;//No collision

}
 
//Then a class that inherits the base class
class MainMenu : public Collision

{

public:

    MainMenu();

    void Draw(sf::RenderWindow &renWin);

private:

    sf::RectangleShape Continue, NewGame, Options, Exit;

};
 
//Code of functions for above class
MainMenu::MainMenu()

{ //Error, no default constructor exists for class "Collision"

    Continue.setFillColor(sf::Color(100,100,100));

    Continue.setPosition(0, 0);

//...

}

Share this post


Link to post
Share on other sites
Advertisement

You need a MainMenu constructor that gets the required RenderWindow* as parameters and use it to construct its base class.

Transate into code:

 

your declaration becomes:

 

MainMenu(sf::RenderWindow* rw);

 

your implementation becomes:

 

MainMenu::MainMenu(sf::RenderWindow* rw) : Collision(rw)

{

// bla bla
}

 

I suggest you to study constructor initializer lists.

Share this post


Link to post
Share on other sites

MainMenu::MainMenu(sf::RenderWindow* rw) : Collision(rw)


 

I suggest you to study constructor initializer lists.

These two things, thanks!

Share this post


Link to post
Share on other sites

The only way to call a constructor of a base class in C++ is through an intialiser list as shown before, this is the same principle as base or super in java. However they work completely opposite from each other. As C# constructs the derived class first and then the base, in C++ however the base gets constructed before the derived class. This is also the reason why you can't call overloaded or virtual methods in the constructor of a C++ class but can in C# and have them being redirected to the derived implementation of that method.

Edited by NightCreature83

Share this post


Link to post
Share on other sites

However they work completely opposite from each other. As C# constructs the derived class first and then the base, in C++ however the base gets constructed before the derived class.

This is almost true, but there's one important detail to be aware of. Although objects do indeed start out as the most derived type, with the method table being for the most derived type, the constructors are still run in order from the base class to the most derived class. This is the most sensible order, since a constructor in a base class must be run before the constructor of any derived class in order for the object to be in a consistent state. Therefore, in C# one has to be extra careful when making virtual method calls in a constructor. If the type is not the most derived type in its inheritance hierarchy, the method will be called on a class whose constructor has not yet been run. (See Stack Overflow) Edited by kloffy

Share this post


Link to post
Share on other sites

Hello.

I have a base class that has a variable that needs to be set when the class object is made.

But when i inherit the class, i have no idea how to invoke the constructor, what are workarounds?

How can constructor of MainMenu class also invoke Collision constructor?

You probably need to get more familiar with C++, since constructor-call order is a basic concept.

 

But, in short, a class will automatically call the default constructor of its base class before it executes its own constructor.  If you provide a constructor, but it is not a default constructor because it requires an input parameter, then any inherited class's constructor has to explicitly call a base class constructor in its initializer list.  So your options are to either do that and call that constructor, or just provide a default one that takes no parameters.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!