Jump to content

  • Log In with Google      Sign In   
  • Create Account


Little help with code, requiring knowledge.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1167

Like
0Likes
Like

Posted 02 May 2013 - 03:22 AM

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

//...

}

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 kunos   Crossbones+   -  Reputation: 2205

Like
3Likes
Like

Posted 02 May 2013 - 04:02 AM

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
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#3 BaneTrapper   Members   -  Reputation: 1167

Like
0Likes
Like

Posted 02 May 2013 - 04:39 AM

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


 

I suggest you to study constructor initializer lists.

These two things, thanks!


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#4 NightCreature83   Crossbones+   -  Reputation: 2746

Like
1Likes
Like

Posted 02 May 2013 - 05:55 AM

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, 02 May 2013 - 05:56 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#5 kloffy   Members   -  Reputation: 896

Like
1Likes
Like

Posted 02 May 2013 - 11:26 AM

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, 02 May 2013 - 11:27 AM.


#6 0r0d   Members   -  Reputation: 815

Like
0Likes
Like

Posted 02 May 2013 - 02:11 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS