Little help with code, requiring knowledge.

Started by
4 comments, last by 0r0d 10 years, 11 months ago

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);

//...

}
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.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

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


I suggest you to study constructor initializer lists.

These two things, thanks!

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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)

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.

This topic is closed to new replies.

Advertisement