Solved Help with Base and Derived Class

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

eduardoroeder

Newbie
Dank Tier Donator
May 2, 2015
30
583
1
Hey guys. I'm having some difficulty on deriving certain functions on a class.
Lets say i have the base class CControl.

C++:
class CControl
{
public:
    CControl() {}
    ~CControl() {}
    void Hey() { 
        MessageBoxA(NULL, "CControl hey", "CControl hey", 0);
    }
protected:
    std::string m_string = "";
};
Then, i define a class CButton based on CControl
C++:
class CButton: public CControl
{
public:
    CButton() { m_string = "ButtonLoaded"; }
    ~CButton() {}
};
Ok. Until now it is fine.

(QUESTION 1) The thing now is that i want to override the Hey() on CControl with a new function on CButton. What do i do?

C++:
class CButton: public CControl
{
public:
    CButton() { m_string = "ButtonLoaded"; }
    ~CButton() {}
    void Hey() { 
        MessageBoxA(NULL, string.c_str(), string.c_str(), 0); // Now it shows the string value
    }
};
Is this right? Ok.

Now, that should work. Yeah.

But then this comes to another problem.

I have multiple classes derived from the CControl class (CButton, CGroupBox, CTextBox...), but i need to put them all inside only one wrapper, so it should be easier to handle some events they all have in common. So I made a vector<CControl> containing all my Controls(either Buttons or GroupBoxes) but it seems that when i do:

C++:
CButton testButton;


void Test_Direct(){
    testButton = CButton();
    testButton.Hey();
// MessageBox says: ButtonLoaded
}

void Test_Vector(){
    std::vector<CControl> Controls = std::vector<CControl>();
    testButton = CButton();
    Controls.pushBack(testButton);
    Controls.at(0).Hey();
// MessageBox says: CControl hey
}
(QUESTION 2) Why does Test_Vector() doesnt execute the overriden function by CButton?

Well, basically i want to be able to call Hey() on the different Controls specifically for the control type, not the base type. How do i do it? I think it have something with Virtual. I tryied playing with that but no sucess.

Thanks!
 
Last edited:

mambda

headass
Escobar Tier VIP
Trump Tier Donator
Jun 25, 2014
2,294
37,938
269
To be able to effectively do that you'd want hey to be a virtual function and then you can define it however you want for any class that derives from ccontrol

C++:
class CControl
{
virtual void Hey() = 0;
}

class CButton : CControl
{
void Hey(){ cout << "Yo" << endl; } 
}
Edit: Example with my old gui code
C++:
#pragma region Element
	class Element {
		/*
		An element needs to have a position relative to the WINDOW.
		Some will have dimensions that are variable, others will have set dimensions.
		Color ( relevant to select areas on certain elements )
		A string that may be used for certain things.
		*/
	protected:
		Vector2D relativePosition, dimensions;
		Color elementColor;
		std::string text;
		Window * parent;
	public:
		Element( );
		Element( Vector2D rel , Vector2D dim , Color col , std::string text , Window * parent );

		void SetPosition( Vector2D v );

		//Relative Position
		const Vector2D GetPosition( );

		void SetColor( Color col );

		void SetText( std::string newText );

		const Vector2D GetDimensions( );

		Window * GetParent( );
		void SetParent( Window * parent );

		virtual void Draw( )		= 0; // Abstraction bois, different draw for different kin folk.
		virtual void Update( )		= 0;
		virtual bool InBounds( Vector2D mousePos )	= 0;
	};
#pragma endregion

#pragma region Tabs
	class Tab : Element {
		// aight, what does a tab need?
		// has the name. it can have width, thats pretty much it i guess.
		// oh cool wait.
		int index;
		bool selected;
		std::vector<Element *> vTabElements;
	public:

		Tab( ) { }
		Tab( int index , UI::Color color , std::string text, Window * parent );

		bool GetSelected( );
		void SetSelected( bool sel );

		void AddElement( Element * ele );

		void Draw( );
		void Update( );
		bool InBounds( UI::Vector2D mousePos );
	};

#pragma endregion
Draw, update and inbounds are the functions to be overridden
 

GAFO666

Hacker
Meme Tier VIP
Aug 19, 2012
520
3,188
23
ewwwww, I dont like it xD So much stuff missing like a few getters, Eventcallbacks etc etc and its always better to put stuff

using Element::xy; (here in tab) into the code, so you can go in the tab header and see all stuff without switching between headers + its more clean
and you reduce the chance of forgetting stuff

whatever if you code alone it doesnt matter after all ^^ And you said its a old code, so you might do it better now
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods